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?
You’re trying to call an instance method (
create_pdf) from a class method (self.perform). Your code would only work if yourDocumentHelperdefinedcreate_pdfas a class method:If you don’t need access to
create_pdfin your views, you may consider moving it to yourDocumentclass instead, as an instance method, and then you can do@doc.create_pdf(pdf).However, if you need access to
create_pdfin your views as well, you can either put amodule_function :create_pdfinside yourDocumentHelperfile, or you can dynamically add this in your worker:Then you can properly call
DocumentHelper.create_pdf.Also, in Rails 3, I think you only need
include DocumentHelper, rather thaninclude ActionView::Helpers::DocumentHelper.