【发布时间】:2011-04-23 12:44:25
【问题描述】:
我是 rails 的新手, 请让我知道注释掉一行并注释掉的方法 *.html.erb 文件中的一行代码。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3
我是 rails 的新手, 请让我知道注释掉一行并注释掉的方法 *.html.erb 文件中的一行代码。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3
ruby on rails notes 有一篇关于在 erb 文件中评论的非常好的博文
短版是
注释单行使用
<%# commented line %>
要评论整个块,请使用if false 来包围您的代码
<% if false %>
code to comment
<% end %>
【讨论】:
请注意,如果您想注释掉一行打印 erb,您应该这样做
<%#= ["Buck", "Papandreou"].join(" you ") %>
【讨论】:
下面也恰好回答了原始发帖人的问题,没有一些评论者提到的“丑陋”条件代码。
连续的非打印 Ruby 代码
这适用于任何混合语言的Rails View文件,例如*.html.erb, *.js.erb, *.rhtml等
这也应该适用于 STD OUT/printing 代码,例如<%#= 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 括号中添加一个注释(hashmark/poundsign)......像这样:
<%#
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
%>
【讨论】:
then。其次,这完全破坏了MVC背后的想法。将您的逻辑保存在助手或控制器中。 -1
if 语句?”。您的回答与 cmets 无关。