views:

122

answers:

3

Using rails 2.3.2 I have a partial _foo.rhtml that begins with a comment as follows:

<% # here is a comment %>
<li><%= foo %></li>

When I render the partial from a view in the traditional way, e.g.

<% some_numbers = [1, 2, 3, 4, 5] %>
<ul>
<%= render :partial => "foo", :collection => some_numbers %>
</ul>

I found that the <li> and </li> tags are ommitted in the output -- i.e. the resulting HTML is

<ul> 1 2 3 4 5 </ul>

However, I can solve this problem by fixing _foo.rhtml to eliminate the space between the <% and the # so that the partial now reads:

<%# here is a comment %>
<li><%= foo %></li>

My question: what's going on here? E.g., is <% # comment %> simply incorrect syntax for including comments in a template? Or is the problem more subtle?

Thanks!

+3  A: 

That is a strange bug, but <%# is the valid comment syntax (without the spaces). It is the same with the other options:

  1. <%= print the output
  2. <%- don't put a line break before this block
  3. <%# ignore this block

None of them should have spaces between the <% and the additional character.

Doug Neiner
+1  A: 

I haven't done the test myself, but I'm under the impression that the code should be generating this:

<ul> 1 </li> 2 </li> 3 </li> 4 </li> 5 </li> </ul>

Which is invalid HTML. Firefox has a tendency of discarding it before showing it to firebug. Are you browsing the generated code through firebug? If yes, I recommend you to "View / Source Code", just to see that the generated code is the same.

My understanding of what happens here is that the # with the space before it "comments out" the %> bit, so you are in effect "still inside ruby" on the next line. It should have given a weird error at that point, though. But it doesn't, so somehow this is accepted as valid ruby code:

<li><%= foo %>

I don't know how this could possibly return the value of foo :/.

The %> that appears on that line is actually the one closing the <% on line one. If I'm correct, then the </li> should also be generated.

If this is not the case, then I'm talking nonsense, so apologies.

egarcia
You are absolutely right -- firebug reported no </li> tags, but checking the source code revealed them exactly as you predicted.
brahn
BTW, others confirmed your answer here: http://www.ruby-forum.com/topic/202330Thanks!
brahn
A: 

I encountered this exact thing yesterday when I tried bootstrapping the code for a new client's project. Interesting thing is that it's happening on 1.8.7 but not on a couple machines that are running 1.8.6. What version of Ruby are you running?

trevrosen
I'm running 1.8.7.
brahn