使用 Sidekiq 和 Wicked PDF 生成 PDF:Worker 的未定义局部变量或方法#
Generating a PDF with Sidekiq & Wicked PDF: Undefined local variable or method for worker#
我一直在尝试在 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.render 方法中没有传递任何局部变量,我也会收到此错误。任何关于导致它的原因的想法都将受到赞赏。
quotes_controller.rb 调用工人
def create_pdf
@quote = Quote.find(params[:id])
GeneratePdfWorker.perform_async(@quote.id, current_account.id)
redirect_to @quote
end
Generate_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_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>
create_pdf.html.erb PDF 查看(为了得到东西 运行 首先,每个本地只用两行)
<%= account.brand_name %>
<%= quote.customer_name %>
非常感谢有关获得此 运行 的任何建议。我试过在 pdf 视图中简单地生成纯 html 文本而不传递任何变量,但仍然出现此错误,所以我对导致它的原因感到困惑。
我有时也会忘记 this - 第 18 行(我只能假设是渲染)似乎非常坚持要在本地查找引用但找不到它,并且@报价是当时定义的。如果这就是您的全部代码,那么我认为这些更改没有被采纳。
我最好的建议(希望有用)是你需要重启 sidekiq!
我一直在尝试在 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.render 方法中没有传递任何局部变量,我也会收到此错误。任何关于导致它的原因的想法都将受到赞赏。 quotes_controller.rb 调用工人
def create_pdf
@quote = Quote.find(params[:id])
GeneratePdfWorker.perform_async(@quote.id, current_account.id)
redirect_to @quote
end
Generate_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_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>
create_pdf.html.erb PDF 查看(为了得到东西 运行 首先,每个本地只用两行)
<%= account.brand_name %>
<%= quote.customer_name %>
非常感谢有关获得此 运行 的任何建议。我试过在 pdf 视图中简单地生成纯 html 文本而不传递任何变量,但仍然出现此错误,所以我对导致它的原因感到困惑。
我有时也会忘记 this - 第 18 行(我只能假设是渲染)似乎非常坚持要在本地查找引用但找不到它,并且@报价是当时定义的。如果这就是您的全部代码,那么我认为这些更改没有被采纳。
我最好的建议(希望有用)是你需要重启 sidekiq!