简体   繁体   English

使用remotipart gem NoMethodError的Rails Jquery上传文件:未定义的方法“名称”

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

I am using gem remotipart for uploading file. 我正在使用gem remotipart上传文件。 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. 但是我得到了这个错误:NoMethodError:nil:NilClass的未定义方法“名称”。 I have no idea from where is the method name and from which Class. 我不知道从哪里来的方法名称和从哪个类。 Any help would be really appreciated. 任何帮助将非常感激。 Thanks 谢谢

At first glance - you have not set :remote => true, which is necessary for the form to work with ajax, so it should be: 乍一看-您尚未设置:remote => true,这对于使用ajax的表单是必需的,因此应为:

<%= 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. 希望这可以帮助。 {: {:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 3 NoMethodError未定义方法“名称” - Rails 3 NoMethodError undefined method `name' Rails 3 NoMethodError:nil:NilClass的未定义方法“名称” - Rails 3 NoMethodError: undefined method `name' for nil:NilClass Rails 3中未定义的方法/ NoMethodError - Undefined method/NoMethodError in Rails 3 Rails 3 / Bundler gem:Bundler的&#39;undefined method`setup&#39;:Module(NoMethodError)&#39; - Rails 3 / Bundler gem: 'undefined method `setup' for Bundler:Module (NoMethodError)' $ rails server…main:Object的未定义方法&#39;gem&#39;(NoMethodError) - $ rails server… undefined method `gem' for main:Object (NoMethodError) rails3-jquery-autocomplete和mongomapper给NoMethodError未定义的方法`table_name` - rails3-jquery-autocomplete and mongomapper gives NoMethodError undefined method `table_name` NoMethodError(未定义的方法`[]&#39;为nil:NilClass):使用jquery-datatables-rails - NoMethodError (undefined method `[]' for nil:NilClass): with jquery-datatables-rails Rails:NoMethodError-未定义的方法“保存” - Rails: NoMethodError - undefined method 'save' NoMethodError:使用 Mocha 和 Rails 3 的未定义方法“模拟” - NoMethodError: undefined method `mock' with Mocha and Rails 3 Ruby on Rails NoMethodError“未定义的方法&#39;translates&#39;” - Ruby on Rails NoMethodError “undefined method `translates'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM