I suspect the problem is how I create the polymorphic image attribute. I am using fields_for
in the form. In this case, the user can create a message and add the image using paperclip, saving it from S3. I am using the polymorphic post_image image model:
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
Message 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 message 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 by Post form_for
: (group elements are not 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
Mail Controller:
def new @user = User.find(params[:user_id])
I suspect the problem is that I am using fields_for
for the post_image attribute, but I looked through everything and cannot figure out how to correctly implement the polymorphic nested image attribute.
I also made a console console s3sh
amazon s3, and although I could not load the image because I could not figure out how to go the correct way of the image to the open () function, I connected to S3, My s3.yml
file s3.yml
also configured correctly.
Thanks yall, Brian