Block comments in html.erb templates in rails

99,254

Solution 1

I wouldn't count as a solution, but perhaps enclosing the chunk between an

<% if false %>
   ...
<% end %>

or if you feel a little dirty, create a helper that simply outputs nothing.

I've never needed it, but I'm stumbled there seems to be no out-of-the-box solution for this.

Solution 2

Use this for commenting single lines:

<%# your_ruby_code %>

For multiple lines, the

<% 
=begin %>  <% ruby_code %>
<% 
=end %>

What you said would work.

Solution 3

The =begin approach is annoying because:

  1. It doesn't work for mixed HTML and Ruby (or just HTML) that's on a single line
  2. It's annoying to type

The <% if false %> approach works, but it looks weird and doesn't give anyone else who looks at your code a hint about your intentions.

My solution is as follows:

In application_helper.rb, add a method so:

def comment
end

Then in your view template, you can say:

<% comment do %>Some stuff that won't be rendered...<% end %>

This works because any Ruby method can take a block, but will silently ignore the passed-in block if your method doesn't include a yield.

Solution 4

<%#=

...commented
multiline
block...

%>

Solution 5

For block comments in templates, my text editor (Komodo) finds this variation on @Garfield's recommendation least obnoxious:

<%# A long multiline comment in a rails template ...
  # line 2
  # and so on ... 
  # %>
Share:
99,254
Nikita Rybak
Author by

Nikita Rybak

Recently joined Google in Australia. Feel free to visit if you happen to be in the office. Author of Newt - question, answer, comment and reputation notification utility for OS X. If you want to say hi, drop me a mail: [first name].[last name]@gmail.com Se vc fala Português, apoia proposta de lingua Português no area 51!

Updated on September 24, 2021

Comments

  • Nikita Rybak
    Nikita Rybak over 2 years

    How do you comment out html mixed with ruby code?

    some text <% ... %> more text <%= ... %>
    something else
    <% ... %>
    

    In jsp it's real simple: <%-- ... --%>, but I'm unable to find any concise option in rails.

    Simple html comments <!-- ... --> do not work: ruby code is still executed and yells errors.

    There's an option to use if false with html comments, but it's quite verbose, not to mention IDEs doesn't support it.

    There's also an option coming from pure ruby, which surprisingly works.

    <%
    =begin %>
    ... html and ruby code goes here
    <%
    =end %>
    

    It's generally fine, except that it's verbose, weird-looking and none of ruby IDEs I know support it (yep, I like to comment/comment-out with one keystroke).

    I'm curious, is there any 'official' of doing this in rails?

    Thanks!

  • Nikita Rybak
    Nikita Rybak almost 14 years
    1) you're right, jsp comments have slightly different format, I updated the post. 2) I can't find anything like that in IDEA or Netbeans. Did you have anything any particular IDE in mind saying this?
  • Nikita Rybak
    Nikita Rybak almost 14 years
    I know it would, I'm interested if there's anything simpler :)
  • John Topley
    John Topley almost 14 years
    No I didn't. Personally, I don't use an IDE for Rails projects.
  • Garfield
    Garfield almost 14 years
    =begin I think the begin - end would be all =end # You may ignore my comment as its entirely commented :P
  • dionyziz
    dionyziz over 10 years
    As far as I understand, he's looking for a multiline comment-out solution: If I have a block of n lines, I just want to be able to add a line at the top and the bottom (or maybe a couple) and have it work. I don't want to have to edit the whole block.
  • dhaval
    dhaval almost 10 years
    it works if = is at start of new line just like in the answer
  • Mariano Cavallo
    Mariano Cavallo over 9 years
    You can even write it as <% comment do %>...<% comment end %>. I've added this syntax to sublime text so that It even looks like a real comment.
  • BenKoshy
    BenKoshy over 8 years
    what if it is like this <%= %> ? Where would the hash go - before or after the equals sign?
  • Adamantish
    Adamantish over 8 years
    Yes, but this still won't comment out both the HTML and the ruby.
  • iono
    iono over 8 years
    Hmm... sounds like good cause for a custom plugin. Maybe as a stopgap, you could press ctrl+d to multi-select the start of every Erb tag <% within the block and then hit ctrl+shift+/, then hit it once more for the whole block to comment out the HTML.
  • Hovis Biddle
    Hovis Biddle over 7 years
    This doesn't work. Any ruby tags inside the block comment will close the outer block.
  • max
    max about 7 years
    You might as well just add a #... instead of sending ruby in the document.
  • ViggoV
    ViggoV almost 6 years
    Oops, tried to add a comment with multiline code. Will post an answer instead.
  • Kieran Andrews
    Kieran Andrews about 5 years
    I am getting this error: "embedded document meets end of file"
  • Ulysse BN
    Ulysse BN almost 5 years
    This answer should have way more interest.
  • tanius
    tanius over 4 years
    Lovely solution!! One improvement: commenting code to disable it is kind of a hack, so rather call the method ignore or disable and we get a fully semantic solution: <% ignore do %>…<% end %>
  • jamesc
    jamesc about 4 years
    @Chloe Not quite sure why you directed your comment to me but you are quite correct <%= false %> would not work. You should try the solution provided though which would work <% if false %> without the = sign
  • Chloe
    Chloe about 4 years
    @jamesc Your comment was deleted before you saw my response. Someone deleted your comment where you said to use <%# if false %>.
  • prashant
    prashant over 3 years
    As of Rails 6 at least, only the first # seems to be necessary.
  • prashant
    prashant over 3 years
    As of Rails 6 at least, the = doesn't seem to be necessary.
  • stevec
    stevec over 3 years
    I tried exactly that but with <% and =begin %> on the same line, which is much more intuitive. Why doesn't that approach work?
  • Mithun Nair
    Mithun Nair about 3 years
    But this wont work when there is ERB code in one of the lines..For Ex
  • stevec
    stevec about 3 years
    The commented block doesn't show grey colour in Sublime Text.