Reputation: 33
I'm building a customer database/invoicing system. When invoices are ready to be converted into printable PDFs using PDFKit, they are selected from the Invoices index through a form containing checkboxes that pass the invoice IDs through the selected_invoices param. I want to piggy back on that method and create the PDF address labels for those specific invoices simultaneously. I can't seem to figure out where I'm going wrong here. When the "labels" method is called, its not able to see the invoice IDs from the params.
Here's the pertinent part of the code:
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
Here's the labels method that gets called by PDFKit:
def labels
@invoices = Invoice.find(params[:selected_invoices])
render :layout => 'labels_layout'
end
The labels method fails, here's what happens in the background:
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'
The rest of the process works fine, I can see the PDFs for the invoices being generated. What am I missing here?
Thanks!
Upvotes: 0
Views: 86
Reputation: 33
I tried passing the parameters or the variable explicitly in a few ways including:
path = labels_invoices_url(@invoices)
and path = labels_invoices_url(params[:selected_invoices]
Both of which gave me similar errors. I tried a different tactic and created two submit buttons on the index page:
<%= submit_tag "Print Selected Invoices" %> <%= submit_tag "Print Labels" %>
I took advantage of each button sending their own commit in the params, and modified the generate_multiple_pdfs
method with an if/else statment to check params[:commit]
:
def generate_multiple_pdfs
if params[:commit] == "Print Labels"
@invoices = Invoice.find(params[:selected_invoices])
render :labels, :layout => 'labels_layout'
else
#do the invoice pdfs
end
end
This doesn't generate the labels in pdf format, and it takes two clicks instead of one, but it works for now.
If someone knows how to pass variables or params to through PDFKit like I was trying, I'd really appreciate their help. Otherwise it looks like I'll be digging around the documentation to see what basics I missed.
Thanks!
Upvotes: 0
Reputation: 1057
try change path = labels_invoices_url
to path = labels_invoices_url(@invoices)
Upvotes: 1