2010-12-11 122 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。