Sometimes it's more convenient to print in <%%>. How to do it in Rails?
-
@JohnTopley A loop which only calls many other partials. – lulalala Oct 03 '13 at 03:57
3 Answers
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-concat
Should be what you are looking for.
E.g. the following statement using concat
:
<% concat "Output" %>
is equivalent to:
<%= "Output" %>

- 2,191
- 19
- 24

- 8,963
- 3
- 33
- 35
-
2
-
2STDOUT goes somewhere else with Rails, you probably *could* do something nasty so that all STDOUT goes to your erb template, but, don't. – Omar Qureshi Dec 07 '15 at 11:18
-
1
-
1@Chloe - STDOUT goes to the log. I've found this default behaviour very handy when running Passenger (for example) where a quick print can help debug. – Nathan Crause Oct 04 '17 at 16:01
-
In ERB: The <% %> signify that there is Ruby code here to be interpreted. The <%= %> says output the ruby code, ie display/print the result.
So it seems you need to use the extra = sign if you want to output in a standard ERB file.
Otherwise, you could look at alternatives to ERB which require less syntax,.. maybe try something like HAML. http://haml-lang.com/tutorial.html
Example:
# ERB
<strong><%= item.title %></strong>
# HAML
%strong= item.title
Is that more convenient?

- 8,939
- 12
- 51
- 63
erb has two method to evaluate inline ruby expressions. The <%
which evaluates the expression and the <%=
which evaluates and prints. There is no global object to print to within the binding context.
As mentioned by Omar, there is a concat method, which is part of ActionView. This will do what you want.
Unlike a scripting language escape, there is no default output for erb. Since erb is simply a function, and given a template and binding will return a variable, it returns the values of text and functions recursively.
There is hot debate as to how much logic should be allowed in a view, but as little as possible is what most people aim for. If you are putting more code than text in the view, you may want to consider refactoring your code.

- 29,240
- 13
- 74
- 99