107

Am a newbie to rails , please let me know the way to comment out a single line and also to comment out a block of lines in *.html.erb files.

1

3 Answers 3

221

ruby on rails notes has a very nice blogpost about commenting in erb-files

the short version is

to comment a single line use

<%# commented line %>

to comment a whole block use a if false to surrond your code like this

<% if false %>
code to comment
<% end %>
4
  • 15
    +1 For solving my issue, but I've found it very ugly. To comment one line I must use 3 additional characters, and the block comment is nothing but code that will be not executed - no other color coding that makes it very unpractical to see which code is not executed on first look.
    – gotqn
    Nov 5, 2012 at 18:34
  • 24
    For single line, you don't need the hyphens e.g. <%# my comment %>
    – jackocnr
    Jan 30, 2013 at 0:50
  • A comment_block helper function would be awesome. Maybe when I gain a little more experience with Rails (I'm pretty new), I will create a pull request Oct 16, 2013 at 7:26
  • 4
    @gotqn Then you will LOVE HAML!
    – Chloe
    Apr 11, 2014 at 18:38
27

Note that if you want to comment out a single line of printing erb you should do like this

<%#= ["Buck", "Papandreou"].join(" you ") %>
1
  • This also works for multiple lines
    – Cadoiz
    Mar 20 at 8:29
8

This is CLEANEST, SIMPLEST ANSWER for CONTIGUOUS NON-PRINTING Ruby Code:

The below also happens to answer the Original Poster's question without, the "ugly" conditional code that some commenters have mentioned.


  1. CONTIGUOUS NON-PRINTING Ruby Code

    • This will work in any mixed language Rails View file, e.g, *.html.erb, *.js.erb, *.rhtml, etc.

    • This should also work with STD OUT/printing code, e.g. <%#= f.label :title %>

    • DETAILS:

      Rather than use rails brackets on each line and commenting in front of each starting bracket as we usually do like this:

        <%# if flash[:myErrors] %>
          <%# if flash[:myErrors].any? %>
            <%# if @post.id.nil? %>
              <%# if @myPost!=-1 %>
                <%# @post = @myPost %>
              <%# else %>
                <%# @post = Post.new %>
              <%# end %>
            <%# end %>
          <%# end %>
        <%# end %>
      

      YOU CAN INSTEAD add only one comment (hashmark/poundsign) to the first open Rails bracket if you write your code as one large block... LIKE THIS:

        <%# 
          if flash[:myErrors] then
            if flash[:myErrors].any? then
              if @post.id.nil? then
                if @myPost!=-1 then
                  @post = @myPost 
                else 
                  @post = Post.new 
                end 
              end 
            end 
          end 
        %>
      
5
  • @TejasKale sorry... not sure what you mean? --> the chances of YOUR code or MY code** working? Apr 10, 2014 at 18:45
  • your method above would generate errors if the code is incorrect, which will almost always be the case with me :)
    – Tejas Kale
    Apr 11, 2014 at 6:32
  • first off, no need for the then. Second, this completely destroy the idea behind MVC. Keep your logic in a helper or in your controller. -1
    – davegson
    Nov 8, 2014 at 9:59
  • @TheChamp I do appreciate you stating why you are downgrading; that's helpful for everyone. But, you are wrong: my answer, adequately addresses the question, and is correct. If you have a BETTER way of doing it, e.g. without the use of then, then you should add your own answer. Beyond that, the OP does not ask about MVC theory, or the Rails paradigm. Finally, if you do believe it is a BAD QUESTION (because commenting out logic breaks Rails MVC best practices) then you should downvote the QUESTION not my answer... Nov 10, 2014 at 12:33
  • @TomLord with respect, yes you are missing something... the if statement is irrelevant.. I am simply using it as an example of a block of code that he might want to comment out, and the use of an OPEN comment tag at the beginning and a CLOSE comment tag at the end Dec 7, 2016 at 12:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.