【问题标题】:Using model attribute as :filename when using paperclip gem使用回形针宝石时将模型属性用作:文件名
【发布时间】:2015-05-18 17:26:51
【问题描述】:

我在尝试将回形针附件的 :filename 更改为等于我将回形针文件附加到的类的属性时遇到错误。

当我使用“#{self.company_name}”时,它会出错。显然在这个范围内,“自我”不是公司。当我写这行时,我假设 self 是我上传这个附件的 Company 的实例。知道如何解决这个问题吗? Paperclip 文档说要使用“:filename”,但我想改用 Company.company_name 的值。

class Company < ActiveRecord::Base
  include AliasAttrs

  has_attached_file :company_logo, {

      :storage => :ftp,

      :path => "/logos/#{self.company_name}",

      :url => FTP_CONFIG[:access_host]+"logos/:filename",

      :ftp_servers => [
        {
          :host     => FTP_CONFIG[:host],
          :user     => FTP_CONFIG[:user],
          :password => FTP_CONFIG[:pass],
          :port     => 21 # optional, 21 by default
        }
      ]
  }
end

更新

我尝试使用这篇文章中的建议:https://robots.thoughtbot.com/paperclip-tips-and-updates

但是现在启动服务器时出现以下错误:

undefined method `interpolations' for Paperclip::Attachment:Class (NoMethodError)

【问题讨论】:

    标签: ruby ruby-on-rails-3 paperclip


    【解决方案1】:

    看起来插值的语法已经改变。更新它并且它工作。将以下内容添加到您的模型中或在 config/initializers 中创建一个 paperclip.rb 文件

      Paperclip.interpolates :company_name do |attachment, style|
        attachment.instance.company_name
      end
    

    【讨论】:

      猜你喜欢
      • 2014-04-13
      • 1970-01-01
      • 2012-12-25
      • 2014-04-28
      • 2012-04-07
      • 1970-01-01
      • 2012-12-22
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多