Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Rails 3 + Polymorphic Image model + Paperclip and Amazon S3, No errors, but nothing uploading

I suspect the problem lies in the way I am creating the polymorphic image attribute. I am using fields_for in the form. In this case a user can create a post and add an image using paperclip, storing it with S3. I am using a polymorphic image model "post_image":

class PostImage < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true
  #.merge(PAPERCLIP_OPS)
  has_attached_file :image, :styles => { :medium => "200x200>", :thumb => "50x50>" },
                            :storage => :s3, 
                            :s3_credentials => "#{Rails.root}/config/s3.yml", 
                            :path => "/:style/:id/:filename", 
                            :bucket => "zounds-dev"
  validates_attachment_presence :image
  validates_attachment_size :image, :less_than => 5.megabytes
end

Post Model:

class Post < ActiveRecord::Base
  has_many :post_images, :as => :imageable, :dependent => :destroy
  .
  .
  .
  accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
end

New Post Form:

=form_for( setup_post(@post,current_user), :html => { :multipart => true}) do |f|
  %dl   
    =f.fields_for :post_images do |ff|
      =ff.file_field :image
    %dt.field=f.label :name
    %dd.field=f.text_field :name
    %dt.field=f.label :description
    %dd.field=f.text_area :description
    =f.fields_for :user do |u|
      =render "user_fields", :f => u
    =f.fields_for :assignments do |ff|
      =ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0','1'
      =ff.label :_destroy, ff.object.group.name
      =ff.hidden_field :group_id
    .action=f.submit "Save Post"

The setup_post helper method used in the Post form_for: (the groups stuff isn't relevant here)

  def setup_post(post, current_user)
    groups = current_user.groups_as_owner + current_user.groups_as_member
    (groups - post.groups).each do |group|
      post.assignments.build(:group => group)
    end
    post.assignments.sort_by {|x| x.group.name }
    post_image = post.post_images.build
    post
  end

Post controller:

  def new
    @user = User.find(params[:user_id])
   #  @post = @user.posts.build
   @post = Post.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @post }
    end
  end

def create
    @user = current_user
    @post = @user.posts.build(params[:post])
    .
    .
    .
end

I suspect the problem is that I am using fields_for for the post_image attribute, but I've looked all over and can't figure out what the proper way to implement a polymorphic nested image attribute is.

I also did the s3sh amazon s3 console thing, and although I couldn't upload an image because I couldn't figure out how to pass in the right image path to the open() function, I connected to S3. My s3.yml file is set up correctly as well.

Thanks yall, Brian

like image 689
Brian Avatar asked Feb 07 '12 02:02

Brian


1 Answers

The issue was with the reject_if in accepts_nested_attributes for the Post model

accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true

commenting it out fixed the issue.

like image 91
Brian Avatar answered Sep 23 '22 17:09

Brian