Dylan Smith

How to write Rails comments that don’t appear in rendered HTML

I ran into a situation last week where a comment in my Rails .html.erb file was being output as an HTML comment. Like almost all of my code comments, I wanted to keep it in the codebase but not visible to snooping devs.

As most Rails developers will probably know, a comment written like this <%# comment %> won’t render in the HTML output. However, a comment like this <% # comment %> will render. The latter, with a space before the #, gets converted to an HTML comment that looks like this <‌!-- comment -->.

If you’re wondering how to get rid of that pesky HTML comment — or add one — check your whitespace.

Newer What should your hourly rate be as a freelancer? Older Southpaw grammar