kcollignon
kcollignon

Reputation: 155

Amazon s3- Paperclip can't upload images to proper path

I have a rails app that users can upload photos to. The photos get uploaded to Amazon S3 bucket. I have a "Contributor" who has many "Listings" which a "Listing" has many "Images". What I would like it to upload all of the images to this type of directory:

:contributor_id/:listing_id/IMAGES_HERE.jpg

I would also like it so that if a contributor creates another listing, it doesn't create a whole entire new folder for that contributor. I want it to upload it to the already created folder with the contributor id.

Any thoughts on how I can achieve this? Here is what my current :path looks like

  has_attached_file :asset,
                :styles => {:large => "640x480", :medium => "300x300", :thumb => "100x100" },
                :storage => :s3,
                :s3_credentials => "#{Rails.root}/config/s3.yml",
                :path => "/:contributor_id/:id/:filename"

ANSWER:

Thanks so much for your answers. here is how I got it to work, using the Interpolations feature of Paperclip.

Paperclip.interpolates('contributor_id') do |attachment, style|
 attachment.instance.listing.contributor_id
end

Paperclip.interpolates('listing_name') do |attachment, style|
 attachment.instance.listing.title.parameterize
end

And then my path..

:path => "/:contributor_id/:listing_name/:filename"

Upvotes: 0

Views: 736

Answers (1)

ABrukish
ABrukish

Reputation: 1452

I'm not sure, but there is like it usually do:

module Paperclip      
  module Interpolations

    def timestamp attachment, style
      attachment.instance_read(:updated_at).to_i
    end

  end
end

And I think it should looks like:

module Paperclip      
   module Interpolations

     def contributor_id attachment, style
       attachment.instance_read(:contributor_id)
     end

   end
end

You could read more about PaperClip's custom interpolations here

Upvotes: 1

Related Questions