Hem*_*nth 89 ruby-on-rails ruby-on-rails-3
作为rails的新手,请让我知道注释单行的方法,并在*.html.erb文件中注释掉一行.
Nik*_*ohl 182
ruby on rails notes有一个关于erb-files评论的非常好的博文
短版是
评论单行使用
<%-# commented line -%>
Run Code Online (Sandbox Code Playgroud)
评论整个块使用a if false
来代替这样的代码
<% if false %>
code to comment
<% end %>
Run Code Online (Sandbox Code Playgroud)
Ger*_*rry 26
请注意,如果你想要注释掉一行打印erb,你应该这样做
<%#= ["Buck", "Papandreou"].join(" you ") %>
Run Code Online (Sandbox Code Playgroud)
下面也恰好回答了原始海报的问题,而没有一些评论者提到的“丑陋”条件代码。
连续的非打印 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 %>
Run Code Online (Sandbox Code Playgroud)
如果您将代码编写为一个大块,则可以改为只向第一个打开的 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
%>
Run Code Online (Sandbox Code Playgroud)