In my ERB files in Rails apps, I’m in the habit of entering comments not meant to be rendered to output like this:
<% # localized to put "showing X through Y of Z" in this block %>
That was working fine for me. However, then I moved a Rails2 app to a new server. And my app was doing weird things, it wasn’t displaying right, and after painful debugging to figure out why, it looked as if certain HTML tags in my .erb were for some reason not being rendered to output. And after more painful debugging, I figured out that all those tags not being output were right after such an attempted one-liner erb ruby comment. I didn’t get an exception, instead it just silently ‘ate’ the subsequent html literal tag in the erb file, possibly seperated by newlines:
<% # localized to put "showing X through Y of Z" in this block %> <div id="foo">
div#foo would be missing from the output.
(Note that your automated tests aren’t neccesarily likely to catch this, unless they happen to be checking for particular HTML tags that happen to follow an attempt at a one-liner erb comment like that. Doh!)
I think the salient change on the new server was ruby 1.8.7 instead of 1.8.6. I was using rails 2.3.5 on both. Other third party gems don’t have exactly the same version on both machines (pre-Bundler, it can be pretty tricky to get exactly the same gem versions on two different deploy environments, and this is a pre-Bundler Rails2 app. Thank god for Bundler.) But none of those other gems seem relevant, my suspicion is that, somehow, it was ruby 1.8.7 vs 1.8.6. Or maybe the particular patch level of 1.8.7 I have, or who knows.
Anyway, the problem is just that particular form of one-liner erb ruby that’s a comment. Putting the erb close tag on another line fixes the problem; oddly, so does using the ERB “-” mark for eating newlines. So either of these forms works fine:
<%- # localized to put "showing X through Y of Z" in this block -%> <% # localized to put "showing X through Y of Z" in this block %>
Learn something new every day, including things I’d rather not have learned.
(update: Yes, it’s an ERB bug (or at least difference in behavior) exhibiting ruby 1.8.7 . Man, sometimes I get rather pessimistic about Rails’ QA, yeah? Thanks anarchivist for the pointer to the ticket. )