views:

57

answers:

2

I often see things like this in rails views:

<% form_tag some_path do -%>      
<% end -%>

Why is there a "-" at the end of each of those lines? My code works fine without it, but is it a best practice or some kind of security measure?

+4  A: 

Adding the "-" to the end of the tag removes the line break for that line, and any whitespace characters that may follow. Likewise, adding it to the beginning removes any whitespace characters that may precede it.

For instance,

Some text.
    <% -%>
More text.

results in:

Some text.
    More text.   # The linebreak in line 2 was suppressed in the output.
John Feminella
A: 

It prevents extra whitespace from being output:

With most of the tags, you can change how ERB trims white space on the same line. Basically the addition of a "-" character to the end of a tag will prevent ERB from including any white space after the tag on the same line to the HTML, including the final carriage return. A "-" at the start of the tag does the same for characters before the tag.

Mark Rushakoff