131

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?

17 Answers 17

176

Use this for commenting single lines:

<%# your_ruby_code %>

For multiple lines, the following you said would work:

<% =begin %>  
<% ruby_code %>
<% =end %>
4
  • 3
    it works if = is at start of new line just like in the answer
    – dhaval
    Jun 20, 2014 at 6:39
  • what if it is like this <%= %> ? Where would the hash go - before or after the equals sign?
    – BenKoshy
    Oct 12, 2015 at 22:37
  • I tried exactly that but with <% and =begin %> on the same line, which is much more intuitive. Why doesn't that approach work?
    – stevec
    Dec 19, 2020 at 5:11
  • Did anyone check if you need a new line before =begin and =end as dhaval said or if @stevec is right and it just works like answered?
    – Cadoiz
    Mar 20 at 8:40
132

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.

2
  • @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
    – jamesc
    Feb 14, 2020 at 10:56
  • @jamesc Your comment was deleted before you saw my response. Someone deleted your comment where you said to use <%# if false %>.
    – Chloe
    Feb 20, 2020 at 8:20
37

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.

2
  • 3
    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. Aug 31, 2014 at 2:36
  • 1
    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 %>
    – tanius
    Oct 12, 2019 at 18:58
16
<%#=

...commented
multiline
block...

%>
4
  • 3
    This answer should have way more interest.
    – Ulysse BN
    May 13, 2019 at 8:39
  • 1
    As of Rails 6 at least, the = doesn't seem to be necessary. Oct 2, 2020 at 19:27
  • @david the <%= is just convention to say it previously was a <%=, not a <%.
    – Cadoiz
    Mar 20 at 8:43
  • @lcjury did you read that he wrote <%=, not <=?
    – Cadoiz
    Mar 20 at 8:44
11

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 ... 
  # %>
2
  • 1
    As of Rails 6 at least, only the first # seems to be necessary. Oct 2, 2020 at 19:27
  • @David You're right, but the point of the answer seems to be that the code editor knows it's comments.
    – Cadoiz
    Mar 20 at 8:45
6

To comment out erb tags use the ruby comment hash symbol before the = sign in the opening tag: <%=. To comment out in HTML, use <!-- -->.

<p>
 1. This is some text I want to keep
 <%= @some_object.some_attribute %>
</p>

<p>
  2. I want to keep this text but comment out the erb tag
  <%#= @some_object.another_attribute %>
</p>

<!--
<p>
  3. I want all of this text commented out including the erb tag
  <%#= @some_object.some_attribute %>
</p>
-->

<!--
<p>
 4. I just want this html commented out but I want to keep the erb tag
 <%= @some_object.some_attribute %>
</p>
-->
1
  • 1
    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.
    – dionyziz
    Oct 23, 2013 at 21:48
5

Since you can use <% %> to put a ruby block, it can be certainly used to put in comments into it.

A simpler and elegant solution would look like...

<%
# See! I am a Ruby Comment
# And I am multi-line
# I look like a recognizable ruby comment block too
# and not so complex
# The only drawback with me is the Hash symbol you have to repeat
# But it's the norm, isn't it?
%>
1
  • 9
    This doesn't work. Any ruby tags inside the block comment will close the outer block. Aug 4, 2016 at 19:20
5

After =begin you do not need to put %>

<%
=begin

code code code code code code 
code code code code code code 
code code code code code code 
code code code code code code 

=end %>
1
  • This one works well for both HTML and Ruby code. Thanks!
    – DariusP
    Jun 9, 2022 at 9:10
5

Just an addendum to some of the previous answers. I found the =begin/=end solution most useful, but for the sake of beauty I write it like so:

<%
=begin
  <p>HTML will be ignored</p>
  <%= 'and so will ruby' %>
  <p>
    <%= 'plus the whole block will be greyed in editor' %>
  </p>
=end
%>

Note that since everything is ignored until the =end there is no need to close the =begin tag with %> or open the =end tag with <% (which has also been pointed out in an earlier answer)

I found this to be the most elegant solution to completely outcomment a block of mixed ruby and html code and have it greyed out in my editor as well, as opposed to the <% if false %> solution. Only drawback is that =begin and =end must be placed at the very beginning of the line..

3
  • 2
    I am getting this error: "embedded document meets end of file" Feb 14, 2019 at 1:26
  • I get that error too. Any solution?
    – Basil
    Aug 22, 2022 at 17:43
  • Isn't this a duplicate of this answer?
    – Cadoiz
    Mar 20 at 8:50
4

Use a HEREDOC called comment

Pros:

  • Self-explanatory that this is a comment
  • Works for erb and HTML tags
  • Has ok syntax highlighting (as one long string)

Cons:

  • Weird 3 line closing syntax
  • No keyboard shortcuts

Code:

The opening tag can be

<% <<-COMMENT %>

the above closing erb tag is just for looks (to match the end),
but don't put anything else there, it may show up on the page

or

<%
<<-COMMENT
%>

Anything here won't run or show up in the browser

<P>
    this will NOT be displayed in the browser
    <strong> not even in the developer's tools </strong>
</p>

<% 1_000_000_000_000.times do |count| %>

for the <%= count %>'th time, this won't run a trillion times,
this is all just a string

all of these %>, <%, <% end %>, end, do, <!--, won't cause any issues.

but the below opening erb tag is important (if you used any erb tags in the comment).
I have no clue why?

The closing tag

yes it needs to be 3 lines 😟. I don't know why the opening erb tag is important but it is! (unless you didn't use any erb tags in the comment).

<%      
COMMENT
%>
3

You can use both <%if false%> and HTML comments at the same time:

<%if false%><--

stuff to comment out

--><%end%>

The benefits are:

  • Ruby code is not executed

  • The commented block has gray color in IDE

  • The intention is obvious for other developers

1
  • The commented block doesn't show grey colour in Sublime Text.
    – stevec
    Mar 28, 2021 at 2:24
2

You have to bear in mind where the code is executed. Ruby-style comments work because the Ruby code is executed on the server before it is served to the web browser. This also explains why HTML comments do not work—the Ruby has already been executed.

Doesn't the IDE you're using support creating custom macros for commenting out blocks of code?

2
  • 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? Jun 27, 2010 at 17:25
  • 1
    No I didn't. Personally, I don't use an IDE for Rails projects. Jun 27, 2010 at 17:45
1

Sublime Text's block comment shortcut ctrl+shift+/ notices whether you've selected normal HTML or an Erb tag and puts either the <!--- or <% =begin %> accordingly.

3
  • Yes, but this still won't comment out both the HTML and the ruby.
    – Adamantish
    Oct 27, 2015 at 19:52
  • 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.
    – iono
    Oct 28, 2015 at 4:16
  • Why is everybody talking about sublime here? It doesn't appear to me as part of the question.
    – Cadoiz
    Mar 20 at 8:56
1
<% %w(
  <span title="<%= title %>">hello</span>
) %>

I hope I've just blown your minds!

0

This is the onlyone that worked for me.

<%
=begin %>

code code code code code code 
code code code code code code 
code code code code code code 
code code code code code code 

<%
=end %>
-1

One way

This is my preferred way.


<%# START COMMENTED OUT SECTION %>
<%if false%><-- 

your view code here....

--><%end%>
<%# END COMMENTED OUT SECTION %>

You might say, why on earth would you want massive caps locks sentences in your code? The answer is because it's easy to forget (or simply not know) what <%if false%><-- is doing, or what --><%end%> is doing. A sleepy or uncaffeinated developer could easily delete them thinking they were typos, which would not be good! That's why I try to be kind to myself/other developers and make it super obvious. It's not succinct or pretty, but it's very practical and almost foolproof.

Second way

This method is great for being:

  • Simple
  • Not idiosyncratic (i.e. uses normally formatted ruby)
  • Expressive: conveys the meaning of what's happening (someone can easily figure out what it's doing)
  • Minimal

And here it is:

<%#

multiple
lines
commented 
out

%>
1
  • But this wont work when there is ERB code in one of the lines..For Ex Feb 3, 2021 at 6:59
-4

The only acceptable solution I ever found to this back-breaking problem was to put a space within the "<%=" to make it no longer register as ruby code, and then comment out the whole block with html comments

Like this:

<!--
<p>
  < %= @some_object.some_attribute %>
</p>
<p>
  < %= @some_object.another_attribute %>
</p>
<p>
  < %= @some_object.some_attribute %>
</p>
<p>
  < %= @some_object.some_attribute %>
</p>
-->

Yes, adding the spaces is annoying. But it is the least annoying of all the solutions I've yet seen.

1
  • 1
    You might as well just add a #... instead of sending ruby in the document.
    – max
    Apr 25, 2017 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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