大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 如何将erb模板渲染为字符串内部动作?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我把它写成了一个单独的erb文件:views / orders / _fax.html.erb,
并尝试渲染erb:html_data = render(:partial =>’fax’).
以下是引发问题的控制器的一部分:
respond_to do |format| if @order.save html_data = render(:partial => 'fax') response = fax_machine.send_fax(html_data) ...... format.html { redirect_to @order,notice: 'Order was successfully created.' } format.json { render json: @order,status: :created,LOCATIOn: @order } else format.html { render action: "new" } format.json { render json: @order.errors,status: :unprocessable_entity } end end
它给了我一个AbstractController :: DoubleRenderError如下:
AbstractController::DoubleRenderError in ordersController#create 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".
如何解决这个问题呢?
ERB docs更详细地解释了这一点.
编辑
要从控制器获取实例变量,请将绑定传递给结果调用,例如:
# controller to_fax(binding) # Helper class def to_fax(controller_binding) html = File.open(path_to_templatE).read template = ERB.new(html) template.result(controller_binding) end
注意:我从来没有这样做过,但似乎可行:)
以上是大佬教程为你收集整理的ruby-on-rails – 如何将erb模板渲染为字符串内部动作?全部内容,希望文章能够帮你解决ruby-on-rails – 如何将erb模板渲染为字符串内部动作?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。