我使用Paperclip来管理Rails 3中某个模型的上传图像。这个模型是另一个模型的附属品。我想让我的图像路径反映这种关系,所以我为此创建了一个自定义插值器。
问题在于,我也想能够编辑
以下是一个简单的示例:
假设我创建了一个名为
如果我决定将
当条目被重命名时,如何强制Paperclip更新所有图像路径?
谢谢!
问题在于,我也想能够编辑
belongs_to
对象的名称,并希望Paperclip相应地重命名文件。以下是一个简单的示例:
class Make < ActiveRecord:Base
attr_accessible :name
has_many :models
end
class Model < ActiveRecord:Base
attr_accessible :img, :make, :name
belongs_to :make
has_attached_file :img, :style => { :thumb => "100x100" },
:path => "/cars/:make_name/:name/:style/:hash.png",
:hash_secret => "blabla"
Paperclip.interpolates :make_name do |attachment, style|
attachment.instance.make.name
end
Paperclip.interpolates :name do |attachment, style|
attachment.instance.name
end
end
假设我创建了一个名为
Chevrolet
和一个型号为Camaro
的汽车,我的图片路径将是/cars/chevrolet/camaro/thumb/my_hash.png
。如果我决定将
Chevrolet
条目名称更改为Chevy
,Rails会尝试在/cars/chevy/camaro/thumb/my_hash.png
位置查找图像,但由于图像未重命名,因此无法找到。当条目被重命名时,如何强制Paperclip更新所有图像路径?
谢谢!