Getting HTML in a controller from a view template

Stackoverflow taught me so much about what the right RESTful, MVC, GET / POST is that I wonder how people learn to program / engineer in the past before Stackoverflow existed;)

Given this, here is another question about how I can make a (fair) general procedure in the most appropriate way.

I need to create HTML from a view template that will be used in a controller action. In this sense, it is similar to ActiveMailer.

  • HTML template in .html.erb file
  • Controller action with parameters
  • Get HTML from a template for use in the controller

What is the best way? Pseudocode will be very appreciated, thanks!

+5
source share
1 answer

Maybe I missed something, but you just need render_to_string?

http://api.rubyonrails.org/classes/ActionController/Base.html#M000465

foo = render_to_string(:template => 'foo/bar', :locals => { :something => 'value' })

This is basically the same as the rendering call in the template, but is written to the string (foo), not the HTTP response.

+11
source

All Articles