Rails + CarrierWave:NoMethodError:nil的未定义方法`name’:NilClass

我正在使用CarrierWave和Rails 3.1。 我在提交表单(尝试上传图片)时收到以下错误消息:

错误信息:

ActiveRecord::StatementInvalid in Admin::PostsController#create NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO "posts" ("body", "created_at", "draft", "image", "post_type", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?) Rails.root: /Users/aziz/Sandbox/ruby/rails/Tumblelog Application Trace | Framework Trace | Full Trace app/controllers/admin/posts_controller.rb:18:in `create' Request Parameters: {"utf8"=>"✓", "authenticity_token"=>"za+zNRDGNCcujnCmO726cWCo2ze1rgaXv5bL17JGaek=", "post"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x000001014aeff0 @original_filename="AzizLight.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"AzizLight.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#>, "draft"=>"0", "user_id"=>"2", "post_type"=>"image"}, "commit"=>"Post"} 

问题是我不知道这个name来自哪里,我不知道哪个变量是零 ,所以我无法正常调试(我试着先调试一个日志然后再问这里)。 第18行对应于以下控制器中的@post.save行:

PostsController:

 # ... def new @post = Post.new @form_html_options = (params[:post_type] == "image") ? { :multipart => true } : {} @form_partial = get_form_partial(params[:post_type]) redirect_to admin_posts_path, :alert => "You tried to create an unknown type of post..." if @form_partial.nil? @title = "Creating a new post..." end def create @post = Post.new(params[:post]) if @post.save flash[:success] = "Post created successfully!" redirect_to admin_post_path(@post) else @title = "Creating a new post..." @form_partial = get_form_partial(params[:post][:post_type]) render 'new' end end # ... 

这里可能需要其他文件来发现问题:

邮政(型号):

 attr_accessible :title, :body, :user_id, :draft, :post_type, :image belongs_to :user mount_uploader :image_url, ImageUploader 

ImageUploader:

 class ImageUploader < CarrierWave::Uploader::Base include CarrierWave::RMagick storage :fog def extension_white_list %w(jpg jpeg gif png) end end 

new.html.erb:

 

admin_posts_path, :html => @form_html_options do |f| %> f %>

_form.html.erb:

   f %>   @post.user_id || current_user.id %>  @post.post_type || params[:post_type] %> 

'Posting...' %>

_image_form.html.erb( @form_partial ):

 

那真正发生了什么呢?

您的图像上传器类未加载到当前的rails服务器线程中。 重新加载rails服务器,它应该工作正常=)。

确保在模型中使用 – mount_uploader:image,ImageUploader,如下所示 –

 class CarImage < ActiveRecord::Base belongs_to :car mount_uploader :image, ImageUploader end 

问候

罗比

我遇到了一些问题,原因是(可能总是)UploadedFile对象已经被发送到了载波wave的安装状态。 db适配器无法序列化此对象,因此会抛出此错误。

确保:

  • 上传器已正确安装
  • 你不使用write_attribute写上传的文件(这是我的问题的原因)。 请改用存取器: model.send('image=', params[:model][:image]) 。 Uglier,但更好。

确保您的模型:

 mount_uploader :image, ImageLoader 

请记住:image必须是字符串/文本类型。

我遇到过这篇文章,因为我遇到了你所描述的相同错误,但是重新启动服务器并没有解决它(正如其他答案所示)。 在我的情况下,问题是由于我在mount_uploader 之前使用了 attr_accessible 。 通过切换它我解决了问题。

我有类似的问题。 解决方案正在改变

 attr_accessible :title, :body, :user_id, :draft, :post_type, :image belongs_to :user mount_uploader :image_url, ImageUploader 

至:

 attr_accessible :title, :body, :user_id, :draft, :post_type, :image_url belongs_to :user mount_uploader :image_url, ImageUploader