来自 Model 的动态样式大小的回形针附件 [英] Paperclip attachments with dynamic style sizes from Model
问题描述
使用 Rails 2,我尝试通过另一个模型与回形针模型分离不同的动态图像大小.我目前使用 Proc 的方法如下所示:
Using Rails 2, I try to separate different, dynamic image sizes trough another Model from the Paperclip-Model. My current approach, using a Proc, looks the following:
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 out fine but apparently no styles are processed and no image is being created.
有人做过这样的事情吗?
Did anyone manage doing stuff like this?
-- 更新 --
显然,实例上的attachment_sizes 方法有时不是为# 定义的——但实例实际上不应该是#?对我来说,这看起来像是在改变实例..
Obviously the method attachment_sizes on instance sometimes is not defined for # - but shouldn't instance actually be #? For me this looks like altering instance..
推荐答案
解决方案很简单.instance
在我的第一个例子中,Proc 是 Paperclip::Attachment 的一个实例.由于我想调用 File
方法,因此必须在 Proc 中获取调用实例:
The solution is simple. instance
in my first example Proc is an instance of Paperclip::Attachment. As I want to call a File
method, one has to get the calling instance inside the Proc:
Proc.new { |clip| clip.instance.attachment_sizes }
instance
表示给定示例中的 File
-instance.
instance
represents File
-instance in the given example.
这篇关于来自 Model 的动态样式大小的回形针附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!