开发者

Paperclip upload issues

开发者 https://www.devze.com 2023-01-30 14:27 出处:网络
I\'m following the following blog list below and I\'m having issues getting paperclip 2.3.6 to work correctly.Not quite for sure why I keep getting this error.I did a \'rails generate paperclip post_i

I'm following the following blog list below and I'm having issues getting paperclip 2.3.6 to work correctly. Not quite for sure why I keep getting this error. I did a 'rails generate paperclip post_images photo' to get paperclip up and running and it seems like everything worked correctly? Any advice would be greatly appreciated.

Blog: http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/

Error:

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"}

Controller Code:

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  

View Code:

=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


Given those params, Post.rb should somewhere have has_attached_file :photo followed by whatever settings you use for Paperclip.


can you show us your Post model? You called the paperclip generator for a post_images model, and it should certainly be Post instead. Look at the paperclip wiki on github.

0

精彩评论

暂无评论...
验证码 换一张
取 消