我正在关注下面的以下博客列表,我遇到了使paperclip 2.3.6正常工作的问题。不确定为什么我一直收到这个错误。我做了一个'rails generate paperclip post_images photo'来启动和运行回形针,看起来一切正常吗?任何建议将不胜感激。
博客:http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
错误:
NoMethodError in Admin/postsController#update
undefined method `photo' for #<Post:0xad91a0c>
Rails.root: /home/kyle/code/BlogMe
Application Trace | Framework Trace | Full Trace
app/controllers/admin/posts_controller.rb:26:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"yEzBbu3wU1owqoJ5RJo4GvzuwT5RUsz5x6/b+6Zo9ns=",
"post"=>{"user_id"=>"1",
"name"=>"test",
"content"=>"test",
"post_images_attributes"=>{"0"=>{"caption"=>"test",
"photo"=>#<ActionDispatch::Http::UploadedFile:0xaf84d8c @original_filename="8d8933735c9079918df1acb9a8ed0a60.jpeg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"post[post_images_attributes][0][photo]\"; filename=\"8d8933735c9079918df1acb9a8ed0a60.jpeg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/tmp/RackMultipart20101210-12711-1l8dre>>}}},
"commit"=>"Update Post",
"id"=>"4"}
控制器代码:
def update
if @post.update_attributes(params[:post])
redirect_to admin_posts_path, :notice => "Updated..."
else
@post.post_images.build
render :action => 'edit'
end
end
查看代码:
=form_for [:admin,@post], :html => {:multipart => true } do |f|
%p
=f.hidden_field :user_id
=f.text_field :name, {:placeholder => "Enter Blog Title Here" }
%p
=f.text_area :content
%p
=f.fields_for :post_images do |builder|
%p
=builder.label :caption, "Image Caption"
=builder.text_field :caption
%p
=builder.label :photo, "Image File"
=builder.file_field :photo
%p
=f.submit
Post.rb
class Post < ActiveRecord::Base
attr_accessible :user_id, :name, :content, :post_images_attributes
has_many :comments
has_many :post_images, :dependent => :destroy
belongs_to :user
validates_presence_of :name
validates_presence_of :content
validates_presence_of :user_id
validates_presence_of :photo
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil? }
end
post_images.rb
class PostImage < ActiveRecord::Base
belongs_to :post
has_attached_file :photo, :styles => { :small => "150x150", :large => "320x240" }
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
end
答案 0 :(得分:1)
考虑到这些参数,Post.rb
应该在某处has_attached_file :photo
后跟您用于Paperclip的任何设置。
答案 1 :(得分:0)