Block comments in html.erb templates in rails
Solution 1
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.
Solution 2
Use this for commenting single lines:
<%# your_ruby_code %>
For multiple lines, the
<%
=begin %> <% ruby_code %>
<%
=end %>
What you said would work.
Solution 3
The =begin
approach is annoying because:
- It doesn't work for mixed HTML and Ruby (or just HTML) that's on a single line
- 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
.
Solution 4
<%#=
...commented
multiline
block...
%>
Solution 5
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 ...
# %>
Nikita Rybak
Recently joined Google in Australia. Feel free to visit if you happen to be in the office. Author of Newt - question, answer, comment and reputation notification utility for OS X. If you want to say hi, drop me a mail: [first name].[last name]@gmail.com Se vc fala Português, apoia proposta de lingua Português no area 51!
Updated on September 24, 2021Comments
-
Nikita Rybak over 2 years
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?
Thanks!
-
Nikita Rybak almost 14 years1) 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?
-
Nikita Rybak almost 14 yearsI know it would, I'm interested if there's anything simpler :)
-
John Topley almost 14 yearsNo I didn't. Personally, I don't use an IDE for Rails projects.
-
Garfield almost 14 years=begin I think the begin - end would be all =end # You may ignore my comment as its entirely commented :P
-
dionyziz over 10 yearsAs 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.
-
dhaval almost 10 yearsit works if = is at start of new line just like in the answer
-
Mariano Cavallo over 9 yearsYou 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.
-
BenKoshy over 8 yearswhat if it is like this <%= %> ? Where would the hash go - before or after the equals sign?
-
Adamantish over 8 yearsYes, but this still won't comment out both the HTML and the ruby.
-
iono over 8 yearsHmm... 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 hitctrl+shift+/
, then hit it once more for the whole block to comment out the HTML. -
Hovis Biddle over 7 yearsThis doesn't work. Any ruby tags inside the block comment will close the outer block.
-
max about 7 yearsYou might as well just add a
#
... instead of sending ruby in the document. -
ViggoV almost 6 yearsOops, tried to add a comment with multiline code. Will post an answer instead.
-
Kieran Andrews about 5 yearsI am getting this error: "embedded document meets end of file"
-
Ulysse BN almost 5 yearsThis answer should have way more interest.
-
tanius over 4 yearsLovely solution!! One improvement: commenting code to disable it is kind of a hack, so rather call the method
ignore
ordisable
and we get a fully semantic solution:<% ignore do %>…<% end %>
-
jamesc about 4 years@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 -
Chloe about 4 years@jamesc Your comment was deleted before you saw my response. Someone deleted your comment where you said to use
<%# if false %>
. -
prashant over 3 yearsAs of Rails 6 at least, only the first
#
seems to be necessary. -
prashant over 3 yearsAs of Rails 6 at least, the
=
doesn't seem to be necessary. -
stevec over 3 yearsI tried exactly that but with
<%
and=begin %>
on the same line, which is much more intuitive. Why doesn't that approach work? -
Mithun Nair about 3 yearsBut this wont work when there is ERB code in one of the lines..For Ex
-
stevec about 3 yearsThe commented block doesn't show grey colour in Sublime Text.