简体   繁体   中英

How to render erb template to string inside action?

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

Use the #render_to_string method

it works the same way as the typical render method but useful when you need to add some templated HTML to a json response

http://apidock.com/rails/ActionController/Base/render_to_string

If you don't want to escape html, just call .html_safe on it:

"<html><body>Hello World</body></html>".html_safe

Re your error, please post your OrdersController - looks like you are calling render or redirect more than once in the create action.

(Btw, just in case you are trying it - you can't render a partial in a controller - you can only render partials in views)

Edit: yeah your problem is you trying to render a partial in the controller action. You could use an after_create callback to set up and send the fax - though again you won't want to use a partial (as they are for views). http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Edit: for your fax problem,you could create a normal Ruby Class, see this excellent bit of advice from Yehuda: https://stackoverflow.com/a/1071510/468009

The reason is you cannot render or redirect inside the same action more than once at a given time.

But in your code, you have both render and redirect . I think in your controller you can use simply only the render, assuming you don't need any json output.

Try this

def create
  @order.save   
  render(:partial => 'fax')
end

I haven't tested this, but I guess you get the idea :), and think about a way to handle errors as well (in case order didn't save).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM