铁轨 + CarrierWave:NoMethodError:未定义的方法“名称”对于 nil: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=#<File:/var/folders/ky/2ddtbt0d7k1g__2ctr8njcfc0000gn/T/RackMultipart20110918-21704-hp2ajt>>,
"draft"=>"0",
"user_id"=>"2",
"post_type"=>"image"},
"commit"=>"Post"}
问题是我不知道这个名称
来自哪里,而且我不知道不知道哪个变量为零,所以我无法正确调试(在此处询问之前我尝试调试日志)。 第 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
# ...
这里是发现问题可能需要的其他文件:
Post (model):
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>
那么到底发生了什么?
I am using CarrierWave with Rails 3.1. I am getting the following error message when I submit the form (trying to upload an image):
Error Message:
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"}
The problem is that I don't know where this name
comes from and I don't know what variable is being nil, so I can't debug properly (I tried to debug a log before asking here).
Line 18 corresponds to the @post.save
line in the following controller:
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
# ...
Here other files that might be needed to spot the problem:
Post (model):
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>
So what it really going on please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的图像上传器类未加载到当前的 Rails 服务器线程中。重新加载 Rails 服务器,它应该可以正常工作 =)。
Your image uploader class was not loaded into your current rails server thread. Reload rails server and it should work fine =).
确保您在模型中使用 - mount_uploader :image, ImageUploader -
问候
Robbie
Make sure you use - mount_uploader :image, ImageUploader in your model like here -
Regards
Robbie
我遇到了一些问题,原因是(可能总是)UploadedFile 对象已发送到安装了 Carrierwave 的属性。数据库适配器无法序列化此对象,因此将引发此错误。
确保:
model.send('image=', params[:model][:image])
。更丑,但更好。I experienced the some problem and the cause is (probably always) that the UploadedFile object has been sent to the attribute carrierwave was mounted on. The db adapter cannot serialize this object and will therefore throw this error.
Make sure that:
write_attribute
to write the uploaded file (which was the cause of my problem). Use the accessor instead:model.send('image=', params[:model][:image])
. Uglier, but better.确保在您的模型中:
记住 :image 必须是字符串/文本类型。
Make sure in your model:
Remember that :image must be the string/text type.
我遇到这篇文章是因为我遇到了与您描述的相同的错误,但重新启动服务器并没有解决它(如其他答案所建议的)。就我而言,问题是因为我在
attr_accessible
之前使用了mount_uploader
。通过切换它们我解决了问题。I came across this post because I had the same error you described, but restarting the server didn't resolve it (as suggested by other answers). In my case, the problem was caused because I used
mount_uploader
beforeattr_accessible
. By switching them I solved the problem.我有类似的问题。
解决方案正在更改:
为:
I had a similar problem.
Solution was changing:
to: