Im trying to create an dynamic bucket name depending on my polymorphic association type.
My first approach was trying something like this:
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image, :styles => { :small => "200x200>", :thumb => "50x50>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => self.imageable_type.to_s
end
If i try to create a new object i got the next error:
NoMethodError: undefined method `imageable_type' for #< Class:0x007fd3fe0b15d8
I find out on the S3 documentation this:
bucket: This is the name of the S3 bucket that will store your files. Remember that the bucket must be unique across all of Amazon S3. If the bucket does not exist Paperclip will attempt to create it. The bucket name will not be interpolated. You can define the bucket as a Proc if you want to determine it's name at runtime. Paperclip will call that Proc with attachment as the only argument.
The problem is that i don't get how i can get this working to set the name of my polymorphic association as the name of the bucket.
Any help will be appreciated.
Hope it help somebody,
The final solution was based on this post: rails paperclip S3 with dynamic bucket name
Read the post for get a better explanation of how use the Proc.
The final code:
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image, :styles => {
:square=> "170x170#",
:rectangle=> "220x170#",
:large => "600x400#" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => lambda { |attachment| "#{attachment.instance.imageable_type.to_s.downcase}-gallery" }
end
Related
Having terrible trouble with my application image rendering since an upgrade to Ruby 1.9.3, Rails 3.1.0.rc4 and Paperclip 3.4.0.
No matter what variation of settings I give Paperclip the infile linked to below comes out blurred as is shown by the linked outfile.
The outfile must fit into a box of 620x412 as shown here.
Link to input file
Link to output file this code generates
The full code for the model is below ...
class Propertyimage < ActiveRecord::Base
belongs_to :property
validates_presence_of :description
validates_presence_of :sortorder
has_attached_file :image, :styles => { :export => {:geometry => "620x412#", :quality => 100, :format => 'JPG'} },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
end
I had a similar problem and was able to fix after a lot of trial and error using three criteria: image size specification, convert_options, and scaling the image. For example, in your Propertyimage class try:
has_attached_file :image,
:styles => { :original => ["640x480", :jpg], :export => {:geometry => "620x412#", :quality => 100, :format => 'JPG'} },
:convert_options => { :all => "-quality 100" },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
Then you can play with the sizes of your image tag, or as in my case using a PDF, Id used the scale option:
pdf.image the_file_name, :at => [0, 720], :scale => 0.75
I am using paperclip to upload photos in my two rails app which share a single database. Now the problem is, if I upload a photo in app-a, for instance, paperclip gives me a url as:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-a/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Here is what I set up in my item model:
class Item < ActiveRecord::Base
attr_accessible :photo, :photo_url, :poll_id, :brand, :number_of_votes
has_attached_file :photo,
:styles => { :thumbnail => "100x100#",
:small => "150x150>",
:medium => "250x250>",
:large => "400x400>" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url=>"/item_:id/created_at_:created_at/:style.jpg"
Paperclip.interpolates :created_at do |attachment, style|
attachment.instance.created_at
end
end
In the other app app-b, when I query the url with item.photo.url(:large), it gave me:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-b/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Therefore I got a wrong url.
Are there any ways I can do that by configuring paperclip? Thanks!
As described here : https://github.com/thoughtbot/paperclip#defaults , you can change the default config, in your case I guess you want to change the :local_root so it doesn't include the app name.
I'm using paperclip inside a project.
The concerned model look like this :
class Asset < ActiveRecord::Base
has_paper_trail # Track model with paper_trail
has_attached_file :image, :styles => { :medium => "300x300>"},
:path => ":rails_root/public/attachments/project_#{:project_id}/ressources/:basename_:style.:extension"
belongs_to :project
end
As you see I want to have the id of concerned project in my image path, how can I make this ?
Try this
has_attached_file :image, :styles => { :medium => "300x300>"},
path => ":rails_root/public/attachments/:project_id/ressources/:basename_:style.:extension"
and then write
Paperclip.interpolates :project_id do |attachment, style|
"project_#{attachment.instance.project_id}"
end
I'm trying to seed my database with member profiles and also member profile pictures with S3 and paperclip but it doesn't seem to be working.
I can create/edit existing members within the application to add pictures with paperclip + S3 and it works just fine but seeding it doesn't work. I have searched but can't find an answer.
I don't know what is your exact problem but you can try something like this in your seeds.rb file :
u = User.new({:name => 'username', :email => 'user#name.fr'...})
u.avartar = File.open('/Users/myAccount/avatars/user.png')
u.save!
In your User.rb file, you must have parperclip configured to work with amazon s3
has_attached_file :avatar,
:styles => { :large => "177x177>", :thumb => "60x60>" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/avatars/:style/:id/:filename"
You could find on dogan kaya berktas blog post detail about s3.yml
I've been having some troubles making custom Interpolation, gone through every example I could find on web, but no matter what I did, had no success.
At the moment I have this:
Model
has_attached_file :photo,
:path => ":rails_root/public/images/:img_name-:style.:extension",
:styles => {
:original => '100x100',
:thumb => '30x30'
}
initializers/paperclip.rb
Paperclip.interpolates :img_name do |attachment, style|
attachment.instance.img_name
end
img_name is field populated in form on upload with the image.
The error I get on upload is:
Invalid argument - (C:/Users/.../stream20110410-384-stl2lk20110230-213-1fm2bab, C:/.../photo_upload/public/images/:img_name-original.jpg)
Seems to work if it's directly in the model:
class Model < ActiveRecord::Base
Paperclip.interpolates :img_name do |attachment, style|
attachment.instance.img_name
end
has_attached_file :photo,
:path => ":rails_root/public/images/:img_name-:style.:extension",
:styles => {
:original => '100x100',
:thumb => '30x30'
}
end