2014-02-10 2 views
0

Я следую руководству railscasts. Я попытался воспроизвести одно из своих учебников, посвященное импортированию CSV и файла Excel на страницу рельсов, но я получаю ошибку круговой зависимости.Обнаружена циклическая зависимость при автозагрузке константа Продукт

следующие решения пытались, но не решить проблему:

  1. приказал повторно драгоценные камни и расслоение обновления/установил его
  2. Пониженный мои рельсы от 4.0.2 до 4.0.0

Другие решения, пожалуйста. Заранее спасибо.

Вид: test.html.erb

<% provide(:title, 'CSV Import Test')%> 

<div class="center hero-unit"> 
<h3> Test - CSV file import </h3> 
</div> 

<h4> Import .csv file </h4> 

<%= form_tag import_products_path, multipart: true do %> 
    <%= file_field_tag :file %> 
    <%= submit_tag "Import" %> 
<% end %> 

<h4> Products </h4> 
<table id="products"> 
    <tr> 
    <th> Id </th> 
    <th> Tagnumber </th> 
    <th> IsEnabled </th> 
    </tr> 

<% @products.each do |product| %> 
<tr> 
    <td><%= product.id_pass %></td> 
    <td><%= product.tagnumber %></td> 
    <td><%= product.isenabled %></td> 
</tr> 
<% end %> 

</table> 

Модель: product.rb

Class Product < ActiveRecord::Base 
    attr_accessible :id_pass, :tagnumber, :isenabled 

    def self.import(file) 
     CSV.foreach(file.path, headers: true) do |row| 
     Product.create! row.to_hash 
    end 
    end 

Контроллер: products_controller.rb

class ProductsController < ApplicationController 
    def test 
    @products = Product.order(:id_pass) 
    end 

    def import 
    Product.import(params[:file]) 
    redirect_to root_url, notice: "Data imported." 
    end 
end 

Config: routes.rb

resources :products do 
    collection { post :import} 
end 

.. 
.. 
match '/test', to: 'products#test', via: 'get' 

ответ

0

Хорошо, эти драгоценные камни работают действительно очень хорошо:

https://github.com/liangwenke/to_xls-rails

Они очень простые камни, но они делают эту задачу очень быстрый. Я написал четкий учебник для рельсов 4 здесь:

https://github.com/liangwenke/to_xls-rails/wiki/Install%20Guide

Вы также можете следовать инструкциям авторов, но я нашел их трудно.

В контроллере:

class ProductsController < ApplicationController 
    def test 
     @products = Product.order(:id_pass) 

     respond_to do |format| 
     format.xls { send_data(@posts.to_xls) } 
     # format.xls { 
     # filename = "products-#{Time.now.strftime("%Y%m%d%H%M%S")}.xls" 
     # send_data(@products.to_xls, :type => "application/excel; charset=utf-8; header=present", :filename => filename) 
     # } 
     end 
    end 
    end 

На ваш взгляд:

<%= link_to "Excel", product_path(:format => :xls) %> 
Смежные вопросы