Best way to add comments in erb
Asked Answered
S

8

283

How do we add comments in erb files, if we do not want them to be generated into the html content?

Scorpion answered 5/5, 2010 at 15:57 Comment(0)
C
401

Use the <%# %> sequence, e.g.

<%# This is a great comment! %>
Cartomancy answered 5/5, 2010 at 15:59 Comment(10)
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..Centavo
It's one of only a few supported ERB tags. 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.Cartomancy
i.e. there can't be any space between <% and #Cartomancy
How can i get textmate to generate these style of comments with apple-/Toad
@Mike Ctrl+Shift+Period (i.e. Ctrl+GreaterThan) 4 times quickly should do it.Cartomancy
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 :PGoblin
@TarynEast: be sure there aren't any characters between <% and #Cartomancy
I have some problem commenting this <li><%= link_to "News", news_path %></li> ... since I have "%>" already in the erb. How should I comment "<li><%= link_to "News", news_path %></li> "??Kolomna
By the way, for those using sublimetext2 .. check out github.com/eddorre/SublimeERBIyeyasu
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?Arpeggio
K
53

For Record

<%# This is a great comment! %>

<%#= This is a great comment! %>
Kuibyshev answered 5/5, 2010 at 16:25 Comment(4)
The <%= -> <%#= example is useful. It doesn't require a special case and isn't documented. It works and is the easiest, though! (Beats having to guess whether there was an = when uncommenting it.)Philharmonic
What's the difference between the two?Nebulize
@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.Selfaddressed
Here's more info on What is the difference difference between '<%#' and '<%#=' in ERB comments?Filaria
P
35

For block comments:

<% if false %>
    code to be commented out...
<% end %> 
Paronymous answered 19/1, 2014 at 18:3 Comment(2)
For blocks you can just add line breaks inside the <%# %> tag.Emelda
Would probably still give errors for broken code in the commentHemisphere
H
7

I have a Windows setup, and this <%-# %> sequence is the only one that works for me:

Example:

<%-# This is a sample comment! %>
Hurlbut answered 26/10, 2011 at 18:56 Comment(0)
P
4

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.

Phonsa answered 30/5, 2019 at 4:32 Comment(2)
This 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.Tumulus
what is command in this context?Feltner
M
3

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.

Metachromatism answered 5/2, 2015 at 10:36 Comment(2)
This does not work <html> <body> <form method="POST" action="www.some-url.com"> <input id="data" name="data" value="<%=# "String" %>"> <input type="submit" value="Send"> </form> </body> </html> throws an errorCoaster
@Coaster 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' %>"Theda
P
1

I doesn't work in the Controllers files, I had to put it between slashes

/ comment here.... /

Petulah answered 27/3, 2019 at 21:20 Comment(0)
J
0

In Embedded Ruby (ERB), which is commonly used in Ruby on Rails applications, you can add comments using the <%# ... %>

Julianajuliane answered 31/1 at 17:33 Comment(1)
Thank you for contributing to StackOverflow! The answer you provided is good by itself, but is just a duplicate of the already accepted answer. In these cases, just upvote the accepted answer instead of posting a duplicate.Sackville

© 2022 - 2024 — McMap. All rights reserved.