代码之家  ›  专栏  ›  技术社区  ›  user7629374

使用Sidekiq和Wicked PDF生成PDF:worker的未定义局部变量或方法#

  •  0
  • user7629374  · 技术社区  · 7 年前

    我一直在尝试在rails 5.1应用程序中通过sidekiq和wicked_PDF生成PDF,但一直出现以下错误:

    2017-11-01T02:20:33.339Z 1780 TID-ovys2t32c GeneratePdfWorker JID-b3e9487113db23d65b179b1c INFO: start
    2017-11-01T02:20:33.369Z 1780 TID-ovys2t32c GeneratePdfWorker JID-b3e9487113db23d65b179b1c INFO: fail: 0.03 sec
    2017-11-01T02:20:33.371Z 1780 TID-ovys2t32c WARN: {"class":"GeneratePdfWorker","args":[2,1],"retry":false,"queue":"default","jid":"b3e9487113db23d65b179b1c","created_at":1509502833.334234,"enqueued_at":1509502833.3345}
    2017-11-01T02:20:33.380Z 1780 TID-ovys2t32c WARN: NameError: undefined local variable or method `quote' for #<GeneratePdfWorker:0x007fb6d5cea070>
    Did you mean?  @quote
    2017-11-01T02:20:33.380Z 1780 TID-ovys2t32c WARN: /Users/stefanbullivant/quottes/app/workers/generate_pdf_worker.rb:18:in `perform'
    

    即使av中没有传递局部变量,我也会出现这个错误。渲染方法。如果您有任何关于是什么原因的想法,我们将不胜感激。 quotes\u控制器。rb呼叫工人

      def create_pdf
        @quote = Quote.find(params[:id])
        GeneratePdfWorker.perform_async(@quote.id, current_account.id)
        redirect_to @quote
      end
    

    生成pdf_worker。rb型

    class GeneratePdfWorker
      include Sidekiq::Worker
      sidekiq_options retry: false
    
      def perform(quote_id, account_id)
        @quote = Quote.find(quote_id)
        @account = Account.find(account_id)
    
        # create an instance of ActionView, so we can use the render method outside of a controller
        av = ActionView::Base.new()
        av.view_paths = ActionController::Base.view_paths
    
        # need these in case your view constructs any links or references any helper methods.
        av.class_eval do
          include Rails.application.routes.url_helpers
          include ApplicationHelper
        end
    
        pdf = av.render pdf: "Quote ##{ @quote.id } for #{ @quote.customer_name }",
                       file: "#{ Rails.root }/tmp/pdfs/quote_#{@quote.id}_#{@quote.customer_name}.pdf",
                   template: 'quotes/create_pdf.html.erb',
                     layout: 'layouts/quotes_pdf.html.erb',
                disposition: 'attachment',
         disable_javascript: true,
             enable_plugins: false,
                     locals: {
                              quote: @quote,
                              account: @account
                            }
    
        # pdf_html = av.render :template => "quotes/create_pdf.html.erb",
        # :layout => "layouts/quotes_pdf.html.erb",
        # :locals => {
        #   quote: @quote,
        #   account: @account
        # }
    
        # use wicked_pdf gem to create PDF from the doc HTML
        quote_pdf = WickedPdf.new.pdf_from_string(pdf, :page_size => 'A4')
    
        # save PDF to disk
        pdf_path = Rails.root.join('tmp', "quote.pdf")
        File.open(pdf_path, 'wb') do |file|
          file << quote_pdf
        en
      end
    end
    

    quotes\u pdf。html。erb PDF布局

    <!DOCTYPE html>
    <html>
      <head>
        <title>Quottes</title>
        <meta charset="utf-8" />
        <meta name="ROBOTS" content="NOODP" />
    
        <style>
          <%= wicked_pdf_stylesheet_link_tag 'quote_pdf' -%>
        </style>
      </head>
    
      <body>
        <div class="container">
          <%= yield %>
        </div>
      </body>
    </html>
    

    创建pdf。html。erb PDF视图(为了让事情先运行,每个本地只需要两行)

    <%= account.brand_name %>
    <%= quote.customer_name %>
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   oowowaee    7 年前

    我有时会忘记 this 同样,似乎第18行(我只能假设它是render)正在查找引号local,但找不到它,并且@quote是在那个时候定义的。如果这些都是你的代码,那么我认为这些更改没有被接受。

    我最好的建议(我希望能奏效)是你需要重新启动sidekiq!