ruby-on-rails - 未调用自定义回形针处理器

标签 ruby-on-rails ruby paperclip

我有一个使用 Devise 生成的用户模型。我正在使用回形针扩展此模型以启用文件上传以及使用自定义回形针处理器处理文件。

我的回形针字段在用户模型中声明如下。 PaperClipStorage 是我使用回形针变量创建的散列。此外,存储在 AWS S3 上。

has_attached_file :rb_resume, PaperclipStorageHash.merge(:style => { :contents => 'resume_contents'}, :processors => [:resume_builder])
validates_attachment_content_type :rb_resume, :if => lambda { |x| x.rb_resume? }, :content_type => ['application/pdf', 'application/x-pdf', 'application/msword', 'application/x-doc']

正在进行 validates_attachment_content_type 检查以确保它只处理 pdf 和 MS word 文件。

我的处理器如下所示

module Paperclip
  class ResumeBuilder < Processor
    def initialize(file,options = {}, attachment = nil)
      @file = file
      @attachment = attachment
      puts "Attachment is not null " if !attachment.nil?
    end

    def make
      rb = MyModule::MyClass.new(@file.path) ### Do something with the file
      section_layout  = rb.parse_html
      @attachment.instance_write(:whiny, section_layout)
      @file
    end
  end
end

在我的用户模型中,我还有一个 after_save 回调,它应该采用在处理器 make 方法中生成的 section_layout。代码如下

after_save :save_sections
def save_sections
   section_layout = rb_resume.instance_read(:whiny)
   # Do something with section_layout...
end

现在我的问题是处理器代码从未被调用,我不知道为什么。

因此 section_layout 变量始终为 nil。

另一点需要注意的是,同一个模型还有另外两个has_attached_file属性。其他两个都不使用自定义处理器。

过去 3 个小时我一直在为此苦苦挣扎。任何帮助将不胜感激。

谢谢

保罗

最佳答案

我的 has_attached_file 声明错误

has_attached_file :rb_resume, PaperclipStorageHash.merge(:style => { :contents => 'resume_contents'}, :processors => [:resume_builder])

实际上应该是

has_attached_file :rb_resume, PaperclipStorageHash.merge(:styles => { :contents => 'resume_contents'}, :processors => [:resume_builder])

注意与单数风格相对的复数风格

关于ruby-on-rails - 未调用自定义回形针处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9092384/

相关文章:

javascript - RoutingError 没有路由匹配 [GET] "/missing.png"

ruby-on-rails - rails : Appending URL parameters & removing URL parameters

ruby - 如何在包含的模块中使用我的 Sinatra 助手?

ruby-on-rails - 测试环境 - 回形针错误

ruby-on-rails - Paperclip Gem - 如何获取生成的文件名的哈希值?

ruby-on-rails - rails : Blocking an user using routes. rb

ruby-on-rails - 一次创建多个多态记录 rails

ruby - 在一个模块中,是否有任何保留的类名?

ruby-on-rails - 用于处理大型现有 Rails 应用程序的建议/工具?

javascript - 带 ActionCable 的回形针