7

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

错误信息:

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是从哪里来的,也不知道什么变量是 nil,所以我无法正确调试(我在询问之前尝试调试日志)。第 18 行对应@post.save于以下控制器中的行:

帖子控制器:

# ...

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

图片上传:

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  storage :fog

  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

新的.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>

那么它到底是怎么回事呢?

4

6 回答 6

4

Your image uploader class was not loaded into your current rails server thread. Reload rails server and it should work fine =).

于 2011-11-14T07:59:33.290 回答
2

我遇到了一些问题,原因是(可能总是)UploadedFile 对象已发送到属性carrierwave 已安装。db 适配器无法序列化这个对象,因此会抛出这个错误。

确保:

  • 上传器已正确安装
  • 你不用write_attribute来写上传的文件(这是我的问题的原因)。请改用访问器:model.send('image=', params[:model][:image]). 更丑,但更好。
于 2011-11-29T12:34:04.337 回答
2

确保你在你的模型中使用 - mount_uploader :image, ImageUploader 像这里 -

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

问候

罗比

于 2011-10-10T13:25:36.790 回答
2

确保在您的模型中:

mount_uploader :image, ImageLoader

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

于 2013-12-26T22:33:33.527 回答
1

我遇到了这篇文章,因为我遇到了您描述的相同错误,但是重新启动服务器并没有解决它(正如其他答案所建议的那样)。就我而言,问题是因为我之前使用mount_uploader attr_accessible. 通过切换它们,我解决了问题。

于 2013-01-26T22:01:02.437 回答
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
于 2013-08-29T19:11:43.330 回答