【问题标题】:Create dynamic directory for file upload in ROR在 ROR 中为文件上传创建动态目录
【发布时间】:2015-07-21 13:06:33
【问题描述】:

我正在使用 mongoid 回形针上传文档,上传时我需要选择目录(它将动态创建新文件夹或现有文件夹),并且需要删除和创建文件夹,有什么想法吗?以前有人做过吗?

【问题讨论】:

  • 您可以为has_mongoid_attached_file 使用:path 选项。默认情况下,将自动创建缺少的目录。如果要清理目录,可以在模型的回调中进行,如before_createafter_destroy

标签: ruby-on-rails ruby mongodb ruby-on-rails-4


【解决方案1】:

如果您将文档目录存储在记录本身中,您可以使用回形针插值来实现您想要的。

您可以这样做(代码未经测试,使用风险自负):

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

More about Paperclip interpolations.

【讨论】:

  • 我也做过类似的事情,但是我需要在目录中创建 dir,打开它们来获取文件..
猜你喜欢
  • 2014-08-09
  • 2014-05-31
  • 2014-11-10
  • 2012-09-23
  • 1970-01-01
  • 2020-08-12
  • 2018-06-13
  • 2020-12-21
  • 1970-01-01
相关资源
最近更新 更多