1

一部のモデルの属性を使用して、has_attached_file パスをカスタマイズしたいと考えています。

これは、ファイルが添付されたモデルです。

class Picture < ActiveRecord::Base

  belongs_to :with_content, :polymorphic => true

  has_attached_file :picture, {
    :styles => { :mobile => ["320x200#", :png], :original => ["1280x1024>", :jpg] },
    :path => ":rails_root/public/:class/:with_content_type/:style_:basename.:extension"
  }
}

ポリモーフィック属性「with_content_type」を使用しようとしていることに注意してください。しかし、うまくいきません。


解決策を見つけました。ものすごく単純。has_attached_file の後に次の簡単なコードを追加します。

 Paperclip.interpolates :with_content_type do |attachment, style|
    "#{attachment.instance.with_content_type}"
  end
4

0 に答える 0