How do you comment out html mixed with ruby code?
some text <% ... %> more text <%= ... %>
something else
<% ... %>
In js
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.
The =begin
approach is annoying because:
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
.
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> 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
<% %w(
<span title="<%= title %>">hello</span>
) %>
I hope I've just blown your minds!
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.
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 ...
# %>