Commenting Out ERb code in Rails (*.html.erb)

by Melvin Ram

Q. Is there an easy way to comment multiple line of ruby in an html.erb file when you have:

...
<% .. %> blah <% .. %>
blah
<% .. %> blah
...

I tried to put <!– –> around it but its still interpreted.

A. There are a few ways. First, you could add a -# at the beginning of the erb tags like this:

<%-# whatever erb code -%>

Secondly, you could  do an if false statement as explain here http://blog.localkinegrinds.com/2008/05/02/erb-block-comments-in-rhtml-templates-using-ruby-on-rails like this:

<% if false %><!-- start erb comment -->
...
<% .. %> blah <% .. %>
blah
<% .. %> blah
...
<!-- end erb comment --><% end %>

This will color code that portion as a comment in text editors and will prevent it from getting send to the browsers at all.

{ 1 comment… read it below or add one }

Renan July 1, 2010 at 12:33 pm

Just what I needed, thanks a lot!

Leave a Comment