8

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?

Jed Schneider
  • 14,085
  • 4
  • 35
  • 46
Meltemi
  • 37,979
  • 50
  • 195
  • 293

5 Answers5

15

<% 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 %>
Mayur Shah
  • 3,344
  • 1
  • 22
  • 41
elc
  • 1,962
  • 20
  • 24
8

I use this all the time

<%# This is a comment %>
brad
  • 31,987
  • 28
  • 102
  • 155
2

The reason Ruby code would be executed inside <!-- --> HTML comments is because all of the server side code (ie. Ruby) is interpreted first, and then the output is sent to the client, at which point the browser interprets <!-- --> as a comment. As the other answers said, use <% #comment %> to comment within a Rails view.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
2

Although (and i'm hoping to be corrected here) you have to be careful because i've had some really strange behavior when doing something like this:

<% if (my_boolean) # Commenting on this if-block %>

where it will affect the HTML that directly follows that (even if it's on another line).

Anyone?

And would this qualify as an answer, or a comment?

ilasno
  • 714
  • 1
  • 13
  • 31
1
<% #comment here %>

:D

jhleath
  • 784
  • 1
  • 8
  • 18