在背景中使用 prawn 生成 pdf 并带有 resque [英] generating pdf using prawn in background with resque

查看:62
本文介绍了在背景中使用 prawn 生成 pdf 并带有 resque的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Resque 后台作业在后台创建一个 PDF 文档.

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

我用于创建 PDF 的代码位于一个 Rails 辅助方法中,我想在 Resque 工作器中使用该方法,例如:

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

create_pdf 方法来自 DocumentHelper,但我收到此错误:

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

undefined method `create_pdf' 

有人知道怎么做吗?

推荐答案

您正在尝试从类方法 (self.perform) 调用实例方法 (create_pdf)>).只有当您的 DocumentHelpercreate_pdf 定义为类方法时,您的代码才会起作用:

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

如果你不需要在你的视图中访问 create_pdf,你可以考虑将它移到你的 Document 类中,作为一个实例方法,然后你可以做@doc.create_pdf(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).

但是,如果您还需要在视图中访问 create_pdf,您可以将 module_function :create_pdf 放入您的 DocumentHelper 文件中,或者您可以在您的工作人员中动态添加它:

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)

然后就可以正常调用DocumentHelper.create_pdf.

另外,在Rails 3中,我认为你只需要include DocumentHelper,而不是include ActionView::Helpers::DocumentHelper.

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

这篇关于在背景中使用 prawn 生成 pdf 并带有 resque的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆