【问题标题】:Multiple render error in rails app while generating pdf file via wicked pdf通过 wicked pdf 生成 pdf 文件时,rails 应用程序中出现多个渲染错误
【发布时间】:2014-12-05 13:11:22
【问题描述】:

我在我的 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")   

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      相关资源
      最近更新 更多