1

これは私のupload.rbです

class Upload < ActiveRecord::Base
      belongs_to :post
      has_attached_file :upload,styles: { medium: ["500x400>",:jpg], thumb: ["100x100>",:jpg]},url: "/post_images/post_:postid/:style/:filename"

def postid
      self.post_id
end
end

post_id の列があります。表すようbelongs_toに、1 つの投稿に対して複数の画像が表示されます。ただし、ファイルをpost_25. として保存しています。post_:postid

しかし、それが機能しているので与えると:id

どうすれば解決できますか。誰でもこれを手伝ってもらえますか。

4

2 に答える 2

3

この機能を実現するには、Paperclip の補間を使用する必要があります。まず、イニシャライザで補間を定義することから始めます。

# config/initializers/interpolations.rb
Paperclip.interpolates :postid do |attachment, style|
  'post_' + attachment.instance.post.id
end

次に、:postid添付ファイルの URL 宣言で補間を直接使用できます (最初にサーバーを再起動することを忘れないでください)。

# app/models/upload.rb
has_attached_file :upload,styles: { medium: ["500x400>",:jpg], thumb: ["100x100>",:jpg]},url: "/post_images/:postid/:style/:filename"

:postidこれは単にモデルで定義したインスタンス メソッドではないことに注意してください。Paperclipは補間を排他的に使用して、URL/パス宣言内で動的変数を定義します。

于 2013-09-24T17:21:07.823 に答える
1

私が持っている私のモデルの1つで

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

これは、Github の Paperclip wiki から取得したものです。

于 2013-09-24T18:42:38.103 に答える