来自Model的动态样式尺寸的回形针附件

使用Rails 2,我尝试通过Paperclip-Model中的另一个模型分离不同的动态图像大小。 我目前的方法,使用Proc,看起来如下:

class File  "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 

一切似乎都很好,但显然没有处理任何样式,也没有创建图像。

有没有人管理这样的事情?

– 更新 –

显然,实例上的attachment_sizes方法有时没有为#定义,但实际上不应该是#? 对我来说,这看起来像改变实例..

解决方案很简单。 在我的第一个例子中,proc是Paperclip :: Attachment的一个实例。 因为我想调用File方法,所以必须在Proc中获取调用实例:

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

instance表示给定示例中的File -instance。

我假设你已经掌握了回形针的所有内容,这样你就已经上传了一张图片,而现在这个过程就不起作用了。

它应该工作。 尽量不要将大小放在数组中。

你这样做

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

但我有它,我没有把大小放在arry中

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

试一试:)