我正在使用 dragonfly gem在我的 Rails 应用程序中管理图像和附件,我需要将图像存储在基于我的特定目录结构中
用户模型。
假设我有一个有名字的用户模型,每个用户都有很多
相册,也有名字,那么我想存储图像
在 "#{RAILS_ROOT}/public/system/#{user.name}/#{user.album.name}/#{suffix}"
我已经设法改变了 dragon fly 的 root_path,我什至 像这样覆盖 relative_storage_path :
class MyDataStore < Dragonfly::DataStorage::FileDataStore
private
def relative_storage_path(suffix)
"#{suffix}"
end
end
但是,我仍然不知道如何传递 ActiveRecord 对象
user.name
和 user.album.name
等属性到 relative_storage_path
创造我理想的道路。
你知道我怎么能做这样的事吗?
最佳答案
gem 作者 Mark Evans 帮了我一个忙,在 google group 上回答了这个问题。这是他的回答,对我的情况非常有效:
你好
你不能开箱即用,因为数据存储是 故意设计得非常简单——你传递数据,它给出 你支持一个 uid 等
如果你想这样做,你必须猴子补丁附件#save!喜欢 所以:
class Dragonfly::ActiveRecordExtensions::Attachment
def save!
destroy! if uid_changed?
self.uid = app.datastore.store(temp_object, parent_model) if has_data_to_store?
end
end
上面我唯一改变的是 datastore.store 需要两个 现在的参数。
然后你必须修改/monkey-patch Dragonfly::DataStorage::FileDataStore#store 考虑到 第二个参数。
出于兴趣,为什么要将图像存储在那个 格式?
干杯 标记
关于ruby-on-rails - Rails + Dragonfly gem : Saving image in a directory structure based on ActiveRecord object attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2705020/