我在我的 rails 应用程序中使用 wicked PDF 来生成 PDF。这是由我的控制器中的私有函数 generate_pdf 完成的,因为在购买过程之后我想生成该 pdf,然后是邮件程序函数将该 pdf 发送给用户。
错误
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".
代码就是这样。
def place_order
... some code
download_new_pdf(@pur)
UserMailer.confirm_order(@pur).deliver
format.html { redirect_to(:action=>'order_confirmed/'+@pur[:id].to_s) }
end
辅助函数将是
def download_new_pdf(id)
@my_ticket = Purchase.find(id)
@event = Event.find(@my_ticket[:event_id])
render :pdf => "#{@my_ticket[:random_no]}.pdf",
:template => 'tickets/download_invoice.html.erb',
:save_to_file => Rails.root.join('public/data/docs', "#{@my_ticket[:random_no]}.pdf")
end
place_order 将完成所有数据库工作,最后生成 pdf,发送邮件并重定向到确认页面。但在这里它给了我多个渲染错误因为
format.html { redirect_to(:action=>'order_confirmed/'+@pur[:id].to_s) }
和
render :pdf => "#{@my_ticket[:random_no]}.pdf",
:template => 'tickets/download_invoice.html.erb',
:save_to_file => Rails.root.join('public/data/docs', "#{@my_ticket[:random_no]}.pdf")
我尝试放置并返回但仍然没有运气,任何帮助将不胜感激。谢谢 !!!