gpt4 book ai didi

ruby-on-rails - 来自 Sidekiq worker : How to pass a partial? 的邪恶 PDF 生成

转载 作者:行者123 更新时间:2023-12-04 06:00:22 25 4
gpt4 key购买 nike

我需要使用 WickedPDF gem 和 Sidekiq 打印 PDF。我认为/希望它几乎可以正常工作,这里是相关的 Controller 代码:

def print_token
@token = Token.find(params[:id])
TokenPdfPrinter.perform_async(@token.id)
redirect_to :back
end

和 worker :

class TokenPdfPrinter
include Sidekiq::Worker
sidekiq_options retry: false

def perform(token_id)
@token = Token.find(token_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: "Token ##{ @token.id }",
file: "#{ Rails.root }/app/admin/pdfs/token_pdf.html.erb",
layout: 'layouts/codes',
page_height: '3.5in',
page_width: '2in',
margin: { top: 2,
bottom: 2,
left: 3,
right: 3 },
disposition: 'attachment',
disable_javascript: true,
enable_plugins: false,
locals: { token: @token }

send_data(pdf, filename: "test.pdf", type: "application/pdf")
end
end

然后局部渲染:

<!DOCTYPE html>
<html>
<head>
<title>WuDii</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<%= wicked_pdf_stylesheet_link_tag "application" %>
<%= wicked_pdf_stylesheet_link_tag "codes" %>
<%= wicked_pdf_javascript_include_tag "application" %>
</head>
<body>
<%= yield %>
</body>
</html>

和token_pdf.html.erb:

<% @token = @token %>
<br>
<div class="barcode text-center"><br><br><br><br><br><br><br><br>
<p><%= @token.encrypted_token_code.scan(/.{4}|.+/).join('-') %></p>
<h3><strong>(<%= number_with_delimiter(@token.credits, delimiter: ',') %>)</strong></h3>
</div>

而且我收到一个错误消息,指出在 token_pdf.html.erb 中@token 为 nil。有什么建议我在这里做错了吗?

最佳答案

您将局部变量 token 传递给 View ,因此您应该在不使用@的情况下引用它。将第一行更改为 <% @token = token %> 它应该可以工作。或者只是跳过第一行并通过局部变量引用标记。

关于ruby-on-rails - 来自 Sidekiq worker : How to pass a partial? 的邪恶 PDF 生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25709785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com