4

モデル値を Paperclip カスタム プロセッサに送信する方法を理解しようとしていますが、これが数日間解決しようとしているため、なぜそれが難しいのか、または解決策が何であるかを理解できません。 ... モデルとプロセッサの両方から抽出したコードを次に示します。

私のモデルから:

...
  has_attached_file :receipt_file,
                    :storage => :s3,
                    :s3_credentials => "#{Rails.root}/config/s3.yml",
                    :path => "/:style/:id/:filename",
                    :s3_protocol => "https",
                    :styles => { :text => { style: :original, receipt_id: self.id }},
                    processors: [:LearnProcessor]
...

「self.id」を使用してレシート ID を取得できないのはなぜですか? のようなものに"/:style/:id/:filename"変換されるのはどうしてですか。/original/1/abc.pdfreceipt_id: :idoptions[:receipt_id]:id1

ある種の補間が必要ですか?

プロセッサ コード

module Paperclip

    class LearnProcessor < Processor
      attr_accessor :receipt_id,:style


      def initialize(file, options = {}, attachment = nil)
        @file           = file
        @current_format = File.extname(@file.path)
        @basename       = File.basename(@file.path, @current_format)
        @style = options[:style]
        @receipt_id = options[:receipt_id]
        puts "Options #{options.inspect}"
      end
...
4

2 に答える 2

1

これが Paperclip 固有の問題かどうかはわかりませんが、対処できる Ruby の問題があります。Ruby では、次のような直感的な DSL を提供するクラス定義でクラス メソッドを呼び出すことができます。

class MyModel < ActiveRecord::Base
  has_attached_file :receipt_file
end

id問題は、このクラス メソッドを呼び出すときにモデルを参照することを期待しているidが、クラスのインスタンスでしか利用できないことです。したがって、これは機能しません。通常、この種のことは、インスタンスが利用可能になると、実行時に評価されるブロックを使用して行われます。

has_attached_file :receipt_file,
                    # ...
                    :styles => { :text => { style: :original, receipt_id: lambda{self.id} }},

ただし、Paperclip は、ブロックを受け入れて呼び出す方法を知る必要があり、そうであるかどうかはわかりません。おそらく、あなたがやろうとしていることを達成する別の方法があり、それが何であるかはわかりませんが、これが役立つことを願っています.

于 2014-04-18T16:20:32.353 に答える
0

これを初期化子に追加します。

module Paperclip
  module Interpolations
    def receipt_id attachment = nil, style_name = nil
      #you should handle the case when attachment and style_name are actually nil
      attachment.instance.receipt_id
    end
  end
end

次に、次のようなパスを持つことができます。

:path => "/:style/:receipt_id/:filename",
于 2012-09-04T18:20:14.007 に答える