comments in ERB gotcha

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

In my old server Blacklight was deployed on, I was using ruby 1.8.6. 

On the new server, I use ruby 1.8.7.  I suspect that is what caused the different behavior, although it could also be a gem that wasn’t exactly the same version on both servers, really it’s hard to explain either way.

Anyway, I was in the habit of doing this sort of thing to make a comment in my .erb source that would not actually get rendered:

<% # localized to put “showing X through Y of Z” in this block %>

That worked fine on my old server. On my new server, it leads to a VERY strange bug where the next HTML tag _following_ such a comment block gets “eaten” and never output.  For instance if write after that <% # comment %> above, there was a couple blank lines followed by <div> tag, that <div> tag never gets output by the app. Weird huh?  Took me a few hours to figure that one out.

However, using the erb ‘-‘ chars to mean ‘eat surrounding newlines’ seems to avoid this weird behavior, as does making sure the closing %> is on a different line, so either of these work:

<% # localized to put “showing X through Y of Z” in this block
%>

<%- # localized to put “showing X through Y of Z” in this block -%>\

Go figure.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s