我一直在尝试在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)
av = ActionView::Base.new()
av.view_paths = ActionController::Base.view_paths
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
}
quote_pdf = WickedPdf.new.pdf_from_string(pdf, :page_size => 'A4')
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 %>