如何将参数发送到回形针处理器

MrW*_*ter 5 ruby arguments ruby-on-rails processor 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]
...
Run Code Online (Sandbox Code Playgroud)

为什么我不能使用“self.id”来获取收据 ID ?它是如何"/:style/:id/:filename"被翻译成类似/original/1/abc.pdf, 如果我输入receipt_id: :id, 我得到的只是options[:receipt_id](见下文):id而不是1

我需要某种插值吗?

处理器代码

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
...
Run Code Online (Sandbox Code Playgroud)

apn*_*ing 1

将其添加到初始值设定项中:

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
Run Code Online (Sandbox Code Playgroud)

然后你可以有这样的路径:

:path => "/:style/:receipt_id/:filename",
Run Code Online (Sandbox Code Playgroud)