Ruby-on-rails 如何注释 rails html.erb 文件中的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3901619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to comment lines in rails html.erb files?
提问by Hemanth
Am a newbie to rails , please let me know the way to comment out a single line and also to comment out a block of lines in *.html.erb files.
我是 rails 的新手,请让我知道如何注释掉一行以及注释掉 *.html.erb 文件中的一行行。
回答by Nikolaus Gradwohl
ruby on rails noteshas a very nice blogpost about commenting in erb-files
ruby on rails notes有一篇关于在 erb 文件中评论的非常好的博文
the short version is
简短版本是
to comment a single line use
注释单行使用
<%-# commented line -%>
to comment a whole block use a if falseto surrond your code like this
要评论整个块,请使用 aif false像这样包围您的代码
<% if false %>
code to comment
<% end %>
回答by Gerry
Note that if you want to comment out a single line of printing erb you should do like this
请注意,如果您想注释掉一行打印 erb,您应该这样做
<%#= ["Buck", "Papandreou"].join(" you ") %>
回答by Flak DiNenno
This is CLEANEST, SIMPLEST ANSWER for CONTIGUOUS NON-PRINTING Ruby Code:
这是连续非打印 Ruby 代码的最干净、最简单的答案:
The below also happens to answer the Original Poster's question without, the "ugly" conditional codethat some commenters have mentioned.
下面也恰好回答了原始海报的问题,而没有一些评论者提到的“丑陋”条件代码。
CONTIGUOUS NON-PRINTING Ruby Code
This will work in any mixedlanguage Rails Viewfile, e.g,
*.html.erb, *.js.erb, *.rhtml, etc.This should also work with STD OUT/printingcode, e.g.
<%#= f.label :title %>DETAILS:
Rather than use rails brackets on each line and commenting in front of each starting bracket as we usually do like this:
<%# if flash[:myErrors] %> <%# if flash[:myErrors].any? %> <%# if @post.id.nil? %> <%# if @myPost!=-1 %> <%# @post = @myPost %> <%# else %> <%# @post = Post.new %> <%# end %> <%# end %> <%# end %> <%# end %>YOU CAN INSTEAD add only one comment (hashmark/poundsign) to the first open Rails bracket if you write your code as one large block... LIKE THIS:
<%# if flash[:myErrors] then if flash[:myErrors].any? then if @post.id.nil? then if @myPost!=-1 then @post = @myPost else @post = Post.new end end end end %>
连续的非打印 Ruby 代码
这适用于任何混合语言的Rails 视图文件,例如
*.html.erb, *.js.erb, *.rhtml,等。这也应该适用于STD OUT/打印代码,例如
<%#= f.label :title %>详情:
而不是像我们通常这样做的那样在每一行上使用 rails 括号并在每个起始括号前注释:
<%# if flash[:myErrors] %> <%# if flash[:myErrors].any? %> <%# if @post.id.nil? %> <%# if @myPost!=-1 %> <%# @post = @myPost %> <%# else %> <%# @post = Post.new %> <%# end %> <%# end %> <%# end %> <%# end %>如果你把你的代码写成一个大块,你可以改为只在第一个打开的 Rails 括号中添加一个注释(哈希标记/井号)......像这样:
<%# if flash[:myErrors] then if flash[:myErrors].any? then if @post.id.nil? then if @myPost!=-1 then @post = @myPost else @post = Post.new end end end end %>

