Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1

ActionView::MissingTemplate after long Ajax request


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.


Solution

  • When sending the CSV file the request being made is for MIME type HTML, not JS (as indicated in the error message :formats=>[:html]). This is probably due to the fact that up until recently it wasn't possible to send files through AJAX requests (it required a work-around).

    If you want to submit the CSV file through an AJAX request take a look at the advice here jQuery Ajax File Upload or google around a bit.

    Otherwise make sure you handle the HTML MIME type.