如何将 erb 模板呈现为字符串内部动作? [英] How to render erb template to string inside action?

查看:31
本文介绍了如何将 erb 模板呈现为字符串内部动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一串 html(类似于 "<html><body>Hello World</body></html>")用于传真目的.

我把它写成一个单独的 erb 文件: views/orders/_fax.html.erb ,并尝试在操作中渲染 erb:html_data = render(:partial => 'fax').

这是引发问题的控制器的一部分:

 respond_to do |format|如果@order.savehtml_data = 渲染(:部分 => '传真')响应 = fax_machine.send_fax(html_data)......format.html { redirect_to @order,注意:'订单已成功创建.'}format.json { 渲染 json:@order,状态::已创建,位置:@order }别的format.html { 渲染动作:新"}format.json { 渲染 json: @order.errors, 状态: :unprocessable_entity }结尾结尾

它给了我一个 AbstractController::DoubleRenderError 如下:

AbstractController::DoubleRenderError in OrdersController#create在此操作中多次调用渲染和/或重定向.请注意,您只能调用渲染或重定向,并且每个操作最多只能调用一次.还要注意,redirect 和 render 都不会终止动作的执行,所以如果你想在重定向后退出动作,你需要做一些类似redirect_to(...) 并返回"的事情.

如何解决这个问题?

解决方案

如果您只需要呈现的 HTML,并且不需要控制器的任何功能,您可以尝试直接在辅助类中使用 ERB,例如:

模块 FaxHelperdef to_faxhtml = File.open(path_to_template).read模板 = ERB.new(html)模板结果结尾结尾

ERB 文档更详细地解释了这一点.>

编辑

要从控制器获取实例变量,请将绑定传递到result 调用中,例如:

# 控制器to_fax(绑定)# 辅助类def to_fax(controller_binding)html = File.open(path_to_template).read模板 = ERB.new(html)模板结果(控制器绑定)结尾

注意:我从未这样做过,但似乎可行:)

I need a string of html (something like "<html><body>Hello World</body></html>") for faxing purpose.

I wrote it into a seprate erb file: views/orders/_fax.html.erb , and try to render the erb in action: html_data = render(:partial => 'fax').

Here is part of the controller that raises the issue:

  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

It gave me an AbstractController::DoubleRenderError as below:

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

How to solve this problem?

解决方案

If you only need the rendered HTML, and don't need any functionality from the controller, you might try using ERB directly within a helper class, eg.:

module FaxHelper

  def to_fax
    html = File.open(path_to_template).read
    template = ERB.new(html)
    template.result
  end

end

The ERB docs explain this in more detail.

EDIT

To get the instance variables from the controller, pass the binding into the result call, eg:

# 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

Note: I've never done this, but it seems workable :)

这篇关于如何将 erb 模板呈现为字符串内部动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆