通过邪恶的pdf生成pdf文件时rails应用程序中的多个渲染错误

时间:2014-12-05 13:11:22

标签: ruby-on-rails ruby wicked-pdf wicked-gem

我在我的rails应用程序中使用了邪恶的PDF来生成PDF。这是由我的控制器中的私有函数generate_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,发送邮件并重定向到配置页面。但是在这里它给了我多个渲染错误coz

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

我尝试放置并返回,但仍然没有运气,任何帮助将不胜感激。谢谢!!!

1 个答案:

答案 0 :(得分:0)

您的错误很简单。您将在download_new_pdf中呈现并同时在place_order中重定向。看起来您需要创建pdf文件并存储在文件系统中,而不是立即向用户显示,因此,请检查readme,这里解释了如何创建PDF并且不渲染它到客户端