Clip attachments with dynamic style sizes from the model

Using Rails 2, I am trying to separate different, dynamic image sizes through another model from the Paperclip model. My current approach using Proc is as follows:

class File < ActiveRecord::Base has_many :sizes, :class_name => "FileSize" has_attached_file( :attachment, :styles => Proc.new { |instance| instance.attachment_sizes } ) def attachment_sizes sizes = { :thumb => ["100x100"] } self.sizes.each do |size| sizes[:"#{size.id}"] = ["#{size.width}x#{size.height}"] end sizes end end class FileSize < ActiveRecord::Base belongs_to :file after_create :reprocess after_destroy :reprocess private def reprocess self.file.attachment.reprocess! end end 

Everything seems to work just fine, but apparently not a single style is being processed and the image is not created.

Has anyone helped do such things?

- Update -

Obviously, the attachment_sizes method in an instance is sometimes not defined for #, but should not be an actual # instance? For me it looks like a modifying instance.

+4
source share
2 answers

The solution is simple. instance in my first example, Proc is an instance of the Paperclip :: Attachment application. Since I want to call the File method, I need to get the calling instance inside Proc:

 Proc.new { |clip| clip.instance.attachment_sizes } 

instance represents the File instance in this example.

+2
source

I assume that you have everything that works with paperclip, so you uploaded the image and now proc just doesn't work.

It should work. Do not put the size in an array.

You do it

 sizes = { :thumb => ["100x100"] } 

But I have this where I do not put the size in arry

 sizes = { :thumb => "100x100" } 

Try it :)

+1
source

All Articles