How to comment lines in rails html.erb files?

Ruby on-RailsRuby on-Rails-3

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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 %>

Solution 2 - Ruby on-Rails

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

<%#= ["Buck", "Papandreou"].join(" you ") %>

Solution 3 - Ruby on-Rails

##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 
          %>
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHemanthView Question on Stackoverflow
Solution 1 - Ruby on-RailsNikolaus GradwohlView Answer on Stackoverflow
Solution 2 - Ruby on-RailsGerryView Answer on Stackoverflow
Solution 3 - Ruby on-RailsFlak DiNennoView Answer on Stackoverflow