Ryan Kanno: The diary of an Enginerd in Hawaii

Everything you've ever thought, but never had the balls to say.

My LinkedIn Profile
Follow @ryankanno on Twitter
My Feed

ERB block comments in RHTML templates using Ruby on Rails

This blog is simply a reminder to myself more than anything else.

After searching the great Googs and reading here, here, and here about commenting out regions in your RHTML templates, I couldn’t find a (good) solution aside from the (<... if false ...>) paradigm. Using Rails 1.2.3 (I know, I know… we’re old school), to get block comments in Rails, the following worked for me, but unfortunately, still wasn’t recognized by NetBeans 6.0 (Boo!).

1
2
3
4
5
6
7
8
9
10
11
12
13
<table>
  ...
  <tbody>
    <tr id="<%= "photographer_#{photographer.id}"%>">
      <td><% 
=begin %>
<%= h photographer.first_name unless photographer.blank? %><% 
=end %>
      </td>
    </tr>
  </tbody>
  ...
</table>

Happy Coding!

Popularity: 49% [?]

  1. Just struggled with this after upgrading to Ruby 1.8.7 and Rails 2.2.2.

    Here is a relevant portion from the inline documentation for ERB:

    | ERB recognizes certain tags in the provided template and converts them based on the rules below:
    |
    |
    |
    |
    | % a line of Ruby code — treated as (optional — see ERB.new)
    | %% replaced with % if first thing on a line and % processing is used
    | — replace with respectively

    So, contrary to some suggestions (http://www.ruby-forum.com/topic/154835#693739), comments ARE allowed in ERB, but it seems to be safe only to do so when the whole embedded expression is a comment, i.e.:

    If your ‘bad comments’ are consistent in their usage of whitespace around the hash mark, then it ought to be easy to do a grep search-and-replace to change all your bad comments to look like this, which WILL work in Ruby 1.8.7:

Please leave a reply »

Powered by Wordpress. Stalk me.