Search code examples
htmlruby-on-railsrubyerbevaluate

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


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.


Solution

  • 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.