0

私はこの(これらの)モデルを持っています

Portfolio
-- PortfolioItem item
  -- Image image
     has_attachment :attachment

すべて素敵なスラッグがあり、インスタンスなどで:attachmentのURLにこの組織を反映させたいと思います

photos/holiday_in_venice/ponte_vecchio (all slugs of the respective hierarchy)

このURLを生成します

photos(??)/holiday_in_venice(??)/:slug/:style.:extension

ペーパークリップパス/URLの作成中にこれらの先行オブジェクトにアクセスするにはどうすればよいですか?

現在、私はしかできません

ponte_vecchio/small.png
4

1 に答える 1

0

カスタム補間を作成する必要があります。その後、とを定義することができ:urlます:path

呼び出さpaperclip.rbれたファイルをconfig/initializers/フォルダに入れて、次のようにします。

Paperclip.interpolates :portfolio_slug do |attachment, style|
  attachment.instance.portfolio_item.portfolio.title.parameterize
end

Paperclip.interpolates :portfolio_slug do |attachment, style|
  attachment.instance.portfolio_item.title.parameterize
end

その後、次のような補間を使用できます。

has_attached_file :attachment, 
  :path=>":rails_root/public/photos/:portfolio_slug/:item_slug/:style.:extension",
  :url=>"/photos/:portfolio_slug/:item_slug/:style.:extension"
于 2012-07-27T06:48:52.953 に答える