Rails ERb best practices (<% %> vs <% -%> vs <%- -%>)

jrdioko picture jrdioko · Nov 8, 2010 · Viewed 11.6k times · Source

What is the recommended use of ERb in Rails when it comes to <% %> (evaluate Ruby code), <% -%> (evaluate Ruby code, suppress the trailing newline) and <%- -%> (evaluate Ruby code, suppress the trailing newline and leading space)? It seems like <%- -%> would make the output HTML look nicest, but <% %> seems to be mostly what I see.

Answer

marko picture marko · Nov 9, 2010

It's a personal preference. I use <% %> when I'm writing a loop or a block, because I want new lines there. I use <% -%> in rare cases of variable assignment. And I never use <%- -%> because that's one option too many.