【发布时间】:2016-04-18 03:57:25
【问题描述】:
我正在构建一个客户数据库/发票系统。当发票准备好使用 PDFKit 转换为可打印的 PDF 时,它们会通过包含复选框的表单从 Invoices 索引中选择,该复选框通过 selected_invoices 参数传递发票 ID。我想采用该方法并同时为这些特定发票创建 PDF 地址标签。我似乎无法弄清楚我要去哪里错了。当调用“标签”方法时,它无法从参数中看到发票 ID。
这是代码的相关部分:
def generate_multiple_pdfs
#generate pdfs from selected invoices and save each to file
@invoices = Invoice.find(params[:selected_invoices])
files = []
@invoices.each do |invoice|
path = show_pdf_invoice_url(invoice)
filename = "invoice_#{invoice.id}.pdf"
files.push filename
kit = PDFKit.new(path)
pdf = kit.to_file("#{Rails.root}/public/invoices/#{filename}")
end
#generate address labels for selected invoices
path = labels_invoices_url
filename = "invoice_labels#{Date.today.to_formatted_s(:iso8601)}.pdf"
files.push filename
kit = PDFKit.new(path)
pdf = kit.to_file("#{Rails.root}/public/invoices/#{filename}")
...
end
这是 PDFKit 调用的标签方法:
def labels
@invoices = Invoice.find(params[:selected_invoices])
render :layout => 'labels_layout'
end
labels 方法失败,这是后台发生的情况:
Processing by InvoicesController#labels as HTML
Invoice Load (0.3ms) SELECT "invoices".* FROM "invoices" WHERE "invoices"."id" = $1 LIMIT 1 [["id", nil]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find Invoice with 'id'=):
app/controllers/invoices_controller.rb:160:in `labels'
流程的其余部分工作正常,我可以看到正在生成的发票的 PDF。我在这里错过了什么?
谢谢!
【问题讨论】:
-
你能说明你是如何称呼标签的吗?表格邮寄到哪里?
-
标签被 pdfkit 调用,使用 labels_invoices_url 路由:
path = labels_invoices_url然后kit = PDFKit.new(path)。我不确定您在询问表单发布到的位置。 -
您必须将参数显式传递给标签方法。参数哈希仅适用于您发布到的方法,所以我猜测表单 POST 到 generate_multiple_pdfs。
标签: ruby-on-rails ruby ruby-on-rails-4