Avoiding space between HTML elements generated by Rails erb template

I discovered this when I was trying to render a bunch of messages to response to an ajax call. The following is my erb template at first, the partial message_entry contains a li element which holds the message:

<% @messages.each do |m| %>
	<%= render 'messages/message_entry', :message => m %>
<% end %>

And I found that the output contains spaces between each li message. In the ajax handler I am using $(data).length to get the count of the message, but with the spaces geneerate between each li, I always get n/2 elements more.

After quite a number of fails with trying this <% -%> kind of notation, I finally found that to avoid the spaces, you just need to write in one line. The following fixes my problem:

<% @messages.each do |m| %><%= render 'messages/message_entry', :message => m %><% end %>

Hope it helps.

comments powered by Disqus