How to comment code in Rails views?

Meltemi picture Meltemi · Jun 22, 2010 · Viewed 9.3k times · Source

As I'm playing with Rails and developing views I often want to comment out code. Simple enough with classes & models but views are a bit more tricky.

What's best way to comment code in a view so it's not interpreted by, well, anything... HTML gives us <!-- commented Rails code here --> though code enclosed here seems to get interpreted anyway?!? Or is there a more Railsy way?

Answer

elc picture elc · Mar 8, 2011

<% code code # comment %> USED to work but I think that was accidental.

You were always supposed to put comments in separate comment tags <%# comment %>

Note NO SPACE before the pound.

Now the old loophole is closed (I forget whether 'now' means Ruby 1.8 or Rails 3 or what) so that:

<% code code  #  this runs too %>
<% # also runs %>
<%# the only way to comment out %>