2010-12-11 124 views
0

我在下面的下面的博客列表,我有问题让paperclip 2.3.6正常工作。不太确定为什么我不断收到这个错误。我做了一个“轨道生成回形针post_images照片”,让回形针启动并运行,它似乎像一切工作正常?任何建议将不胜感激。回形针上传问题

博客: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

您的视图代码是什么?我猜你调用了'<%= builder.label:photo,“图像文件”%><%= builder.file_field:photo%>'是错误的,而你正在使用'f.file_field'而不是'builder .form_field' – iain 2010-12-11 11:32:24

+0

我发表了我的观点。 – 2010-12-11 15:28:09

回答

1

鉴于这些参数,Post.rb应该在某处有has_attached_file :photo后面跟着您用于Paperclip的任何设置。

0

,你能告诉我们你的后模型?你称之为post_images模型的回形针发生器,它肯定应该是Post。看看github上的paperclip wiki。