【发布时间】:2010-09-18 16:24:07
【问题描述】:
Rails 3 在视图中注释一行或多行代码的方法是什么?所以它不会出现在 HTML 源代码中
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3
Rails 3 在视图中注释一行或多行代码的方法是什么?所以它不会出现在 HTML 源代码中
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3
要注释掉一行 ob ruby 代码,请使用
<%# code %>
or for multiple lines
<%
=begin
your code
=end
%>
编辑: 这是一个注释掉视图中循环的示例。 =begin 和 =end 必须直接位于行首。 不能有任何空格或制表符。
<h1>Listing posts</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
<th></th>
</tr>
<%
=begin
%>
<%@posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
<%
=end
%>
</table>
【讨论】:
Rails 视图中的 3 行注释:
f.label :name 行已被注释掉:
<%= form_for(@usr) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%#= f.label :name %>
<%= f.text_field :name %>
<%= f.submit "Save changes" %>
<% end %>
因为这是一个视图,所以 # 必须在 之内。
Rails 3 视图中的多行注释:
开始多行注释:
<%
=begin
%>
结束多行注释:
<%
=end
%>
下面,整个 form_for 块已被注释掉:
<%
=begin
%>
<%= form_for(@usr) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit "Save changes" %>
<% end %>
<%
=end
%>
请注意,要使多行注释标签起作用,=begin 或 =end 前面不能有空格或制表符。它们必须位于一行的开头,否则它们将不起作用。
【讨论】:
这是我从 HTML 中隐藏 cmets 的方法(...无论如何,它都有效!)
def comment
# use this keyword in the views, to comment-out stuff...
end
<h3><% comment = "my Rails-y something something here will not be visible to HTML.
I don't know why I have to comment stuff out in this manner.
It's probably not the 'Rails Way' but ...it works for me.
It would be nice if Rails 3 had some kind of comment-out feature like <%## %> or <%-- --%> or something like that...
Ah, well...
At least they won't be able to 'View Source' and read this comment! ;]
" %></h3>
<h3></h3>
C-YA!
【讨论】:
注释代码的一些方法
<%
=begin
%>
RUBY CODE GOES HERE
<%
=end
%>
<% if false %>
RUBY CODE GOES HERE
<% end %>
<%# RUBY CODE%>
<%#= RUBY CODE%>
<!--
HTML CODE
-->
对于模型/控制器等.rb 文件中的 RUBY 代码
def xyz
=begin
blah blah code
=end
end
对于 JS 等
/*
Some code
*/
【讨论】:
您指的是哪些“块”? html?那么你可以使用 红宝石代码?
【讨论】: