【问题标题】:What method does Paperclip use to clean up file name?Paperclip 使用什么方法来清理文件名?
【发布时间】:2013-08-31 01:41:37
【问题描述】:

Thoughtbot 的 Paperclip gem (v 3.2.1) 将清理文件名,用下划线替换空格和其他特殊字符。我正在重新导入数据,需要检查附件是否已经上传,但原始文件名可能与回形针中的附件名不匹配。回形针用什么方法清理?

【问题讨论】:

    标签: ruby-on-rails-3 paperclip


    【解决方案1】:

    找到深入研究源代码的答案。这是一个私有实例方法 Paperclip::Attachment#cleanup_filename 。 (由于这是一个短暂的(一次性)导入任务,我不介意使用未发布方法的(轻微)风险。)

    因此我的代码看起来像这样(Post has_many :attachments; Attachment has_attached_file :attached)

       if @post.attachments.present?
          cleaned_filename = @post.attachments.first.attached.send :cleanup_filename, filename 
          if @post.attachments.map(&:attached_file_name).include? cleaned_filename
            puts "already attached: #{filename}"
            return
          end
        end
        puts "attaching upload: #{filename}"
    

    【讨论】:

    • 我发现这对我有用:cleaned_filename = post.plan_pdf.send(:cleanup_filename, plan_pdf_name) 其中 post 是新记录或在数据库中找到的记录。谢谢! (这不是回答原始问题,只是找到了一种通过您调用 cleanup_filename 的干净方法)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2011-09-04
    • 2019-01-24
    • 2012-02-06
    • 2017-05-07
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多