可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答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.
回答2:
Use this for commenting single lines:
<%# your_ruby_code %>
For multiple lines, the
<%
=begin %> <% ruby_code %>
<%
=end %>
What you said would work.
回答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
.
回答4:
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 ...
# %>
回答5:
<%#=
...commented
multiline
block...
%>
回答6:
Since you can use <% %>
to put a ruby block, it can be certainly used to put in comments into it.
A simpler and elegant solution would look like...
<%
# See! I am a Ruby Comment
# And I am multi-line
# I look like a recognizable ruby comment block too
# and not so complex
# The only drawback with me is the Hash symbol you have to repeat
# But it's the norm, isn't it?
%>
回答7:
To comment out erb tags use the ruby comment hash symbol before the = sign in the opening tag
<p>
This is some text I want to keep
<%= @some_object.some_attribute %>
</p>
<p>
I want to keep this text but comment out the erb tag
<%#= @some_object.another_attribute %>
</p>
<!--
<p>
I want all of this text commented out including the erb tag
<%#= @some_object.some_attribute %>
</p>
-->
<!--
<p>
I just want this html commented out but I want to keep the erb tag
<%= @some_object.some_attribute %>
</p>
-->
回答8:
After =begin you do not need to put %>
<%
=begin
code code code code code code
code code code code code code
code code code code code code
code code code code code code
=end %>
回答9:
You have to bear in mind where the code is executed. Ruby-style comments work because the Ruby code is executed on the server before it is served to the web browser. This also explains why HTML comments do not work—the Ruby has already been executed.
Doesn't the IDE you're using support creating custom macros for commenting out blocks of code?
回答10:
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.
回答11:
This is the onlyone that worked for me.
<%
=begin %>
code code code code code code
code code code code code code
code code code code code code
code code code code code code
=end %>
回答12:
Just an addendum to some of the previous answers. I found the =begin/=end solution most useful, but for the sake of beauty I write it like so:
<%
=begin
<p>HTML will be ignored</p>
<%= 'and so will ruby' %>
<p>
<%= 'plus the whole block will be greyed in editor' %>
</p>
=end
%>
Note that since everything is ignored until the =end
there is no need to close the =begin
tag with %>
or open the =end
tag with <%
(which has also been pointed out in an earlier answer)
I found this to be the most elegant solution to completely outcomment a block of mixed ruby and html code and have it greyed out in my editor as well, as opposed to the <% if false %>
solution. Only drawback is that =begin
and =end
must be placed at the very beginning of the line..
回答13:
You can use both <%if false%> and HTML comments at the same time:
<%if false%><--
stuff to comment out
--><%end%>
The benefits are:
Ruby code is not executed
The commented block has gray color in IDE
The intention is obvious for other developers
回答14:
Use a HEREDOC called comment
Pros:
- Self-explanatory that this is a comment
- Works for erb and HTML tags
- Has ok syntax highlighting (as one long string)
Cons:
- Weird 3 line closing syntax
- No keyboard shortcuts
Code:
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