How do we add comments in erb files, if we do not want them to be generated into the html content?
Asked
Active
Viewed 8.4k times
7 Answers
370
Use the <%# %>
sequence, e.g.
<%# This is a great comment! %>
John Douthat
- 39,186
- 10
- 62
- 65
-
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 .. 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
-
20i.e. there can't be any space between ` – 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 ` – John Douthat Nov 07 '11 at 18:13
-
I have some problem commenting this ... since I have "%>" already in the erb. How should I comment " "?? – 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 suggestion by user user3212755. Does anyone have any other suggestions? – TMin Feb 14 '15 at 16:38
50
For Record
<%# This is a great comment! %>
<%#= This is a great comment! %>
Salil
- 43,381
- 19
- 113
- 148
-
4
-
2
-
3@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
6
I have a Windows setup, and this <%-# %> sequence is the only one that works for me:
Example:
<%-# This is a sample comment! %>
Edward Castaño
- 587
- 1
- 7
- 14
3
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.
Miftah Mizwar
- 3,016
- 2
- 9
- 16
-
1This 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
2
Since .erb is by definition "embedded ruby", you can embed every 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.
Maaaaaaarco
- 21
- 2
-
-
@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=""` – jdigital Nov 15 '15 at 04:59
1
I doesn't work in the Controllers files, I had to put it between slashes
/ comment here.... /
Arezki
- 11
- 2