Reputation: 10207
After upgrading my Rails 4 app to Rails 4.2 I am getting this error:
AbstractController::DoubleRenderError in InvoicesController#download
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
This is the controller in question:
class InvoicesController < ApplicationController
def download
@invoice = Invoice.find_by(:download_code => params[:id])
if @invoice
respond_to do |format|
format.pdf { |pdf| render_pdf("attachment") }
end
else
flash[:notice] = "File cannot be found."
redirect_to signin_path
end
end
private
def render_pdf(disposition = "inline")
pdf = InvoicePdf.new(@invoice, view_context)
options = { :filename => invoice_filename(@invoice), :type => "application/pdf", :disposition => disposition }
send_data(pdf.render, options)
end
end
Any idea what I am missing here?
Thanks for any help.
Upvotes: 1
Views: 407
Reputation: 10207
Turns out that I simply had to add and return
at the end of the line to get this to work. I am not 100% sure why this is necessary, though. Maybe someone can shed some light on this.
respond_to do |format|
format.pdf { |pdf| render_pdf("attachment") and return }
end
Upvotes: 1