Ruby on Rails Tip: Using ERb in Controllers 0
Posted Monday, February 19, 2007 20:33
If you want to render something using a helper inside a controller, render it using the inline option:
render :inline => "<%= any_ERb_here %>"
For example, suppose you want to examine the contents of an object. You can do this easily in a view, if the object is accessible there, using the debug helper, which converts any object to YAML:
<%= debug object_to_be_examined %>
But because debug is a view helper, you can’t normally use it in a controller. You can, of course, pass the object as an instance variable and put a debug statement in a view, but sometimes it is more convenient to just insert this in your controller:
render :inline => "<%= debug object_to_be_examined %>"
This works even if there is no view file at all that corresponds to the action in which you place this. If there is a render or redirect statement later in the action, however, you need to add “and return” after the inline render statement, or you’ll get an error message telling you that there can only be one render or redirect in an action.
