2015-04-12 136 views
1

升級我的Rails 4程式後到Rails 4.2我收到此錯誤後:DoubleRenderError升級到Rails 4.2

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".

這是有問題的控制器:

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 

任何想法,我是什麼在這裏失蹤?

感謝您的任何幫助。

+0

你在'ApplicationController'中潛伏了'before_action'嗎?另一個可能是愚蠢的現成想法:嘗試將'render_pdf'重命名爲'create_pdf'。 – janfoeh

+0

你試過去掉'respond_to'塊並只保留這個'render_pdf(「attachment」)}'? –

+0

@janfoeh:謝謝但不是,在這種情況下沒有'before_action'。重命名也沒有辦法。 – Tintin81

回答

1

原來,我只需在行尾添加and return就可以實現這個功能。不過,我不是100%確定這是必要的。也許有人可以對此有所瞭解。

respond_to do |format| 
    format.pdf { |pdf| render_pdf("attachment") and return } 
end