Rails Jquery upload file using remotipart gem NoMethodError: undefined method `name'

343 Views Asked by At

I am using gem remotipart for uploading file. Here is my view:

<%= form_for @file_upload, :id => "form_feedback_attachment", :url => { :controller => "widgets", :action => "feedback_attachment_upload" }, :html => {:multipart => true} do |f| %>
   <td class="rightcol"><%= f.file_field :feedback_attachment, :id => "feedback_attachment_file" %>
   <%= f.submit "Upload" %></td>
<% end %>

My controller:

@file_upload = FileUpload.new
    @file_upload.feedback_attachment = params[:file_upload][:feedback_attachment]
     respond_to do |format|
      if @file_upload.save
        format.js
      end
    end

But I get this error : NoMethodError: undefined method `name' for nil:NilClass. I have no idea from where is the method name and from which Class. Any help would be really appreciated. Thanks

1

There are 1 best solutions below

0
On

At first glance - you have not set :remote => true, which is necessary for the form to work with ajax, so it should be:

<%= form_for @file_upload, :remote => true, :id => "form_feedback_attachment", :url => { :controller => "widgets", :action => "feedback_attachment_upload" }, :html => {:multipart => true} do |f| %>

At the controller, if you are using this action for something else this may be a problem, so you should wrap it like this:

 if @file_upload
   @file_upload.feedback_attachment = params[:file_upload][:feedback_attachment]
     respond_to do |format|
      if @file_upload.save
        format.js
      end
    end
 end

Also note that i have removed the first line. Hope this helps. {: