0
我使用mongoid回形針上傳文檔,上傳時我需要選擇目錄(它將動態創建新文件夾或現有文件夾),並且需要刪除並創建文件夾,有任何想法嗎?任何人之前做過?在ROR中創建文件上傳的動態目錄
我使用mongoid回形針上傳文檔,上傳時我需要選擇目錄(它將動態創建新文件夾或現有文件夾),並且需要刪除並創建文件夾,有任何想法嗎?任何人之前做過?在ROR中創建文件上傳的動態目錄
如果您將文檔的目錄存儲在記錄本身中,則可以使用Paperclip插值來實現您想要的效果。
這裏是你如何能做到這一點(代碼沒有經過測試,使用風險自負):
class Doc
include Mongoid::Document
field :directory, type: String
# create custom interpolation rule to fetch directory from the record itself
Paperclip.interpolates :document_directory do |attachment, _|
attachment.instance.directory
end
# now you can use :document_directory in your url and path
has_mongoid_attached_file :doc,
url: '/system/:document_directory/:id/:style/:id.:extension',
path: ':rails_root/public/system/:document_directory/:id/:style/:id.:extension',
# other Paperclip options
end
我已經做了類似的工作,但我需要在目錄中創建目錄,打開它們以獲取文件.. – arunwrc
你可以用':爲'has_mongoid_attached_file' path'選項。默認情況下,缺少的目錄將被自動創建。如果你想清理目錄,你可以在模型的回調函數中做到這一點,比如'before_create'和'after_destroy'。 – Aetherus