0

I've got some weird problem with one of my remote actions. I would like to parse CSV file uploaded through form. CSV file is parsing correctly and it takes about 20 sec (its ok, because CSV file is containing urls with download links). After successful parsing my view is not displaying. Funny thing is that view renders when csv file is not selected in form.

#Products Controller:
  def get_imported
     p=Product.new
     @tab = nil
     @tab = p.import_csv(params[:product][:csv_file],params[:product][:source]) unless params[:product][:csv_file].nil?
  end

#app/views/products/get_imported.js.coffee view:
    $('#results').html '<br/><div class="alert alert-info">
    <h4 class="alert-heading">Imported products:</h4>
    <% if @tab.blank? %>
        Error: No CSV file
    <% else %>
        <% for element in @tab %>
            <%= @tab.name %><br/>
        <% end %>
    <% end %>
    </div>'

If I select csv file and submit my form following error displays:

    ActionView::MissingTemplate (Missing template products/get_imported, application/get_imported with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
      * "/home/chmarus/Pulpit/test/app/views"
      * "/home/chmarus/.rvm/gems/ruby-1.9.3-head/gems/twitter-bootstrap-rails-2.1.0/app/views"
      * "/home/chmarus/.rvm/gems/ruby-1.9.3-head/gems/devise-2.1.2/app/views"
      * "/home/chmarus/.rvm/gems/ruby-1.9.3-head/gems/ckeditor-3.7.1/app/views"
    ):

I do not understand why rails cant find this template. In case with no file selected alert renders from same file correctly.

4

1 回答 1

2

发送 CSV 文件时,发出的请求是针对 MIME 类型的 HTML,而不是 JS(如错误消息中所示:formats=>[:html])。这可能是因为直到最近还不能通过 AJAX 请求发送文件(它需要解决方法)。

如果您想通过 AJAX 请求提交 CSV 文件,请查看此处的建议jQuery Ajax File Upload或 google 一下。

否则,请确保您处理 HTML MIME 类型。

于 2012-07-14T16:29:07.517 回答