在后台使用 prawn 和 resque 生成 pdf
我正在尝试通过 Resque 后台作业在后台创建 PDF 文档。
我用于创建 PDF 的代码位于 Rails 辅助方法中,我想在 Resque 工作线程中使用该方法,例如:
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
create_pdf
方法来自 DocumentHelper
但我收到此错误:
undefined method `create_pdf'
有谁知道怎么做吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试从类方法 (
self.perform
) 调用实例方法 (create_pdf
)。仅当您的DocumentHelper
将create_pdf
定义为类方法时,您的代码才有效:如果您不需要在视图中访问
create_pdf
,则您可以可以考虑将其作为实例方法移动到您的Document
类中,然后您可以执行@doc.create_pdf(pdf)
操作。但是,如果您还需要在视图中访问
create_pdf
,您可以将module_function :create_pdf
放入您的DocumentHelper
文件中,或者您可以在您的工作人员中动态添加此内容:然后您可以正确调用
DocumentHelper.create_pdf
。另外,在 Rails 3 中,我认为您只需要
include DocumentHelper
,而不是include ActionView::Helpers::DocumentHelper
。You're trying to call an instance method (
create_pdf
) from a class method (self.perform
). Your code would only work if yourDocumentHelper
definedcreate_pdf
as a class method:If you don't need access to
create_pdf
in your views, you may consider moving it to yourDocument
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 amodule_function :create_pdf
inside yourDocumentHelper
file, 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
.