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

时间:2011-09-18 21:16:17

标签: ruby-on-rails carrierwave nomethoderror

我正在使用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=#<File:/var/folders/ky/2ddtbt0d7k1g__2ctr8njcfc0000gn/T/RackMultipart20110918-21704-hp2ajt>>,
 "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:

<h1><%= @title %></h1>

<%= form_for @post, :url => admin_posts_path, :html => @form_html_options do |f| %>
  <%= render 'form', :f => f %>
<% end %>

_form.html.erb:

<%= render 'error_messages' %>

<%= render @form_partial, :f => f %>

<p class="drop-down">
  <%= f.label :draft, 'Status' %>
  <%= f.select(:draft, options_for_select([["Published", 0], ["Draft", 1]], (@post.new_record? ? 0: @post.draft))) %>
</p>

<%= f.hidden_field :user_id, :value => @post.user_id || current_user.id %>
<%= f.hidden_field :post_type, :value => @post.post_type || params[:post_type] %>

<p class="button"><%= f.submit "Post", :disable_with => 'Posting...' %></p>

_image_form.html.erb(@form_partial):

<p><%= f.file_field :image %></p>

那真的发生了什么?

6 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

请确保您在模型中使用 - mount_uploader:image,ImageUploader -

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

此致

罗比

答案 2 :(得分:2)

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

确保:

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

答案 3 :(得分:2)

确保您的模型:

mount_uploader :image, ImageLoader

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

答案 4 :(得分:1)

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

答案 5 :(得分:0)

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

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