generating pdf using prawn in background with resque

安稳与你 提交于 2019-12-10 09:34:28

问题


I am trying to create a PDF document in the background via Resque background job.

My code for creating the PDF is in a Rails helper method that I want to use in the Resque worker like:

class DocumentCreator
  @queue = :document_creator_queue
  require "prawn"

  def self.perform(id)
    @doc = Document.find(id)

    Prawn::Document.generate('test.pdf') do |pdf|
      include ActionView::Helpers::DocumentHelper
      create_pdf(pdf)
    end
  end
end

The create_pdf method is from the DocumentHelper but I am getting this error:

undefined method `create_pdf' 

Anyone know how to do this?


回答1:


You're trying to call an instance method (create_pdf) from a class method (self.perform). Your code would only work if your DocumentHelper defined create_pdf as a class method:

def self.create_pdf

If you don't need access to create_pdf in your views, you may consider moving it to your Document class instead, as an instance method, and then you can do @doc.create_pdf(pdf).

However, if you need access to create_pdf in your views as well, you can either put a module_function :create_pdf inside your DocumentHelper file, or you can dynamically add this in your worker:

DocumentHelper.module_eval do
  module_function(:create_pdf)
end
DocumentHelper.create_pdf(pdf)

Then you can properly call DocumentHelper.create_pdf.

Also, in Rails 3, I think you only need include DocumentHelper, rather than include ActionView::Helpers::DocumentHelper.



来源:https://stackoverflow.com/questions/6525158/generating-pdf-using-prawn-in-background-with-resque

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!