繁体   English   中英

使用paperclip / rails更改保存文件的路径

[英]Change the path for saving file using paperclip/rails

我正在制作Rails应用程序并使用paperclip将文件上传到AWS S3存储桶。 在我的模型中,我配置如下方式:

 class File < ApplicationRecord
    has_attached_file :attachment,
                                :url => "/sample_pdf/:basename.:extension",
                                :path => "/sample_pdf/:basename.:extension"

    validates_attachment :attachment,
                                         :content_type => {
                                                 :content_type =>
                                                         ["application/pdf"]
                                         }
end

我面临的问题是,有时我需要将文件上传到"/sample_pdf/:basename.:extension" ,有时我需要上传到其他路径,如"/another_pdf_folder/:basename.:extension"

我不确定是否有办法改变存储文件的路径取决于我的需要。

谢谢。

您可以通过模型中的方法设置路径来动态设置路径。

class File < ApplicationRecord
    has_attached_file :attachment,
               :url => "/sample_pdf/:basename.:extension",
               :path => :attachment_dynamic_path,
               validates_attachment :attachment,
               :content_type => {
                                 :content_type =>  ["application/pdf"]
                                }
  def attachment_dynamic_path
    condition ? "/sample_pdf/:basename.:extension" : "/another_pdf_folder/:basename.:extension"
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM