How do we add comments in erb files, if we do not want them to be generated into the html content?
8 Answers
Use the <%# %>
sequence, e.g.
<%# This is a great comment! %>

- 40,711
- 10
- 69
- 66
-
I used to use this format until I noticed it just raised an error on someones computer in my team (we were both using linux, but different distros), regardless I avoid it since.. – vise May 05 '10 at 21:57
-
4It's one of only a few supported ERB tags. http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html If it didn't work on their PC, they either had a typo, or they weren't using real ERB. However, I've run into issues with code like this <% for foo in bar # loop over bar %>.. This was OK in the ERB that shipped with 1.8.6, but when I switched to 1.8.7, it caused lots of problems. – John Douthat May 05 '10 at 22:20
-
23i.e. there can't be any space between `<%` and `#` – John Douthat May 05 '10 at 22:30
-
How can i get textmate to generate these style of comments with apple-/ – Michael Aug 10 '10 at 22:39
-
2@Mike Ctrl+Shift+Period (i.e. Ctrl+GreaterThan) 4 times quickly should do it. – John Douthat Aug 11 '10 at 16:02
-
This occasionally fails in some rubys... I find you need to put the closing "%>" tag on a newline... which feels a bit kludgy, but means you don't suddenly find half your template disappears on production, even though it works find on development :P – Taryn East Nov 07 '11 at 15:55
-
@TarynEast: be sure there aren't any characters between `<%` and `#` – John Douthat Nov 07 '11 at 18:13
-
I have some problem commenting this
- <%= link_to "News", news_path %>
... since I have "%>" already in the erb. How should I comment "- <%= link_to "News", news_path %>
"?? – WowBow Jun 10 '12 at 20:26 -
By the way, for those using sublimetext2 .. check out https://github.com/eddorre/SublimeERB – Abram Dec 27 '12 at 03:25
-
I was using this but found it doesn't work if I want to comment out a section of existing code that already contains a closing tag %> (because the comment ends at the first closing tag). So now I'm using the <% if false %> <%end%> suggestion by user user3212755. Does anyone have any other suggestions? – TMin Feb 14 '15 at 16:38
For Record
<%# This is a great comment! %>
<%#= This is a great comment! %>

- 46,566
- 21
- 122
- 156
-
4The `<%=` -> `<%#=` example is useful. It doesn't require a special case and [isn't documented](http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html). It works and is the easiest, though! (Beats having to guess whether there was an `=` when uncommenting it.) – Benjamin Atkin Mar 04 '12 at 18:52
-
3
-
5@TravisR: The first one is just a comment, the second is also just a comment but probably arises when you're trying to disable a `<%= ... %>` by turning it into a comment. – mu is too short Aug 07 '17 at 04:00
-
Here's more info on [What is the difference difference between '<%#' and '<%#=' in ERB comments?](https://stackoverflow.com/questions/45538995/what-is-the-difference-difference-between-and-in-erb-comments?noredirect=1&lq=1) – sb813322 Dec 30 '22 at 09:09
I have a Windows setup, and this <%-# %> sequence is the only one that works for me:
Example:
<%-# This is a sample comment! %>

- 617
- 2
- 8
- 14
In my text editor, i run command + /
(sublime-text shortcut). It will be like this.
<%
=begin%>
Here is the comment
<%
=end%>
It doesn't look simply, but it works.

- 3,722
- 2
- 13
- 20
-
2This works as hoped in .ERB files where one wants to comment out multiple statements all at once. An explanation of what is actually happening would be a good addition to the answer. – RARay Oct 07 '20 at 20:54
-
Since .erb is "embedded ruby" by definition, you can embed any ruby code between: <%=
and the other: %>
, typically all written in one line. In addition, ruby one-line comments start always with #
, so the <%=# Comment %>
style matches perfectly with both pure-ruby and erb styles for one-line comments.

- 2,165
- 2
- 18
- 39

- 21
- 2
-
-
1@Epigene Likely cause of the error is the inner quotes. Not sure why you'd want to do this, but to get rid of the error, try: `value="<%=# 'String' %>"` – jdigital Nov 15 '15 at 04:59
I doesn't work in the Controllers files, I had to put it between slashes
/ comment here.... /

- 11
- 2
For .html.erb files, you can use the standard HTML multi-line comment
<!--
...erb and html code...
-->
For example,
<!--
<div>
<%= form.label :book_id, style: "display: block" %>
<%= form.text_field :book_id %>
</div>
-->
It also works for a single line,
<!-- <%= 1 %> -->

- 312
- 3
- 15