125

你如何注释掉混合了 ruby​​ 代码的 html?

some text <% ... %> more text <%= ... %>
something else
<% ... %>

在 jsp 中它真的很简单:<%-- ... --%>,但我无法在 rails 中找到任何简洁的选项。

简单的 html 注释<!-- ... -->不起作用:ruby 代码仍在执行并大喊错误。

有一个if false与 html 注释一起使用的选项,但它非常冗长,更不用说 IDE 不支持它。

还有一个来自纯红宝石的选项,它出奇地有效。

<%
=begin %>
... html and ruby code goes here
<%
=end %>

它通常很好,除了它很冗长、看起来很奇怪而且我所知道的所有 ruby​​ IDE 都不支持它(是的,我喜欢用一个键来评论/注释掉)。

我很好奇,在rails中是否有任何“官方”这样做?

谢谢!

4

17 回答 17

172

使用它来注释单行:

<%# your_ruby_code %>

对于多行,

<% 
=begin %>  <% ruby_code %>
<% 
=end %>

你说的会有用。

于 2010-06-27T16:53:42.077 回答
122

我不会算作一个解决方案,但也许将块包含在一个

<% if false %>
   ...
<% end %>

或者如果你觉得有点脏,创建一个不输出任何内容的助手。

我从来不需要它,但我偶然发现似乎没有开箱即用的解决方案。

于 2010-06-27T18:12:58.773 回答
35

这种=begin方法很烦人,因为:

  1. 它不适用于单行的混合 HTML 和 Ruby(或只是 HTML)
  2. 打字很烦

<% if false %>方法有效,但它看起来很奇怪,并且不会向查看您的代码的其他任何人暗示您的意图。

我的解决方案如下:

application_helper.rb中,添加一个方法:

def comment
end

然后在您的视图模板中,您可以说:

<% comment do %>Some stuff that won't be rendered...<% end %>

这是可行的,因为任何 Ruby 方法都可以使用一个块,但如果您的方法不包含yield.

于 2014-06-10T00:29:01.377 回答
12
<%#=

...commented
multiline
block...

%>
于 2014-06-09T10:07:03.567 回答
8

对于模板中的块注释,我的文本编辑器 (Komodo) 发现@Garfield 推荐的这种变化最不讨厌:

<%# A long multiline comment in a rails template ...
  # line 2
  # and so on ... 
  # %>
于 2013-03-21T15:40:15.757 回答
6

要注释掉 erb 标记,请在开始标记中的 = 符号之前使用 ruby​​ 注释井号

<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>
-->
于 2011-07-23T16:28:46.803 回答
5

既然你可以<% %>用来放一个 ruby​​ 块,它当然可以用来在里面放注释。

一个更简单和优雅的解决方案看起来像......

<%
# 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?
%>
于 2014-08-30T12:20:12.940 回答
4

=begin 之后不需要放 %>

<%
=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 %>
于 2016-02-15T13:39:02.743 回答
3

只是对之前一些答案的补充。我发现 =begin/=end 解决方案最有用,但为了美观,我这样写:

<%
=begin
  <p>HTML will be ignored</p>
  <%= 'and so will ruby' %>
  <p>
    <%= 'plus the whole block will be greyed in editor' %>
  </p>
=end
%>

请注意,由于所有内容都被忽略,直到=end不需要关闭=begin标签%>或打开=end标签<%(在较早的答案中也已指出)

我发现这是最优雅的解决方案,可以完全排除混合的 ruby​​ 和 html 代码块,并将其在我的编辑器中显示为灰色,而不是<% if false %>解决方案。唯一的缺点是=begin并且=end必须放在行的开头。

于 2018-05-22T13:00:43.993 回答
3

使用一个名为 comment 的 HEREDOC

优点:

  • 不言自明,这是评论
  • 适用于 erb 和 HTML 标签
  • 语法高亮(作为一个长字符串)

缺点:

  • 奇怪的 3 行结束语法
  • 没有键盘快捷键

代码:

开始标签可以是

<% <<-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

或者

<%
<<-COMMENT
%>

此处的任何内容都不会在浏览器中运行或显示

<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?

结束标签

是的,它必须是 3 行。我不知道为什么开头的 erb 标签很重要,但它很重要!(除非您没有在评论中使用任何 erb 标签)。

<%      
COMMENT
%>
于 2018-07-08T21:32:53.973 回答
2

您可以同时使用 <%if false%> 和 HTML 注释:

<%if false%><--

stuff to comment out

--><%end%>

好处是:

  • Ruby 代码未执行

  • 注释块在 IDE 中为灰色

  • 其他开发人员的意图很明显

于 2018-05-23T23:08:37.970 回答
1

您必须记住代码的执行位置。Ruby 样式的注释之所以有效,是因为 Ruby 代码在提供给 Web 浏览器之前在服务器上执行。这也解释了为什么 HTML 注释不起作用——Ruby 已经被执行了。

您使用的 IDE 不支持创建自定义宏来注释代码块吗?

于 2010-06-27T16:02:20.123 回答
1

Sublime Text 的块注释快捷方式ctrl+shift+/会通知您是否选择了普通 HTML 或 Erb 标签,并相应地放置<!---或放置<% =begin %>

于 2012-12-11T21:46:54.540 回答
0

单程

这是我的首选方式。


<%# START COMMENTED OUT SECTION %>
<%if false%><-- 

your view code here....

--><%end%>
<%# END COMMENTED OUT SECTION %>

你可能会说,你到底为什么要在你的代码中使用大量的大写锁定语句?答案是因为很容易忘记(或根本不知道)<%if false%><-- 正在做什么或--><%end%>正在做什么。一个昏昏欲睡或没有咖啡因的开发人员可以很容易地删除它们,以为它们是错别字,这可不好!这就是为什么我尝试善待自己/其他开发人员并让它变得非常明显。它既不简洁也不漂亮,但它非常实用且几乎万无一失。

第二种方式

这种方法非常适合:

  • 简单的
  • 不是特殊的(即使用正常格式的 ruby​​)
  • 表达性:传达正在发生的事情的含义(有人可以很容易地弄清楚它在做什么)
  • 最小

这里是:

<%#

multiple
lines
commented 
out

%>
于 2020-12-19T05:17:33.077 回答
0
<% %w(
  <span title="<%= title %>">hello</span>
) %>

我希望我刚刚让你大吃一惊!

于 2020-07-16T20:58:25.450 回答
0

这是唯一为我工作的人。

<%
=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 %>

于 2016-02-08T13:38:17.180 回答
-4

对于这个令人费解的问题,我发现的唯一可接受的解决方案是在 "<%=" 中放置一个空格,使其不再注册为 ruby​​ 代码,然后用 html 注释注释掉整个块

像这样:

<!--
<p>
  < %= @some_object.some_attribute %>
</p>
<p>
  < %= @some_object.another_attribute %>
</p>
<p>
  < %= @some_object.some_attribute %>
</p>
<p>
  < %= @some_object.some_attribute %>
</p>
-->

是的,添加空格很烦人。但这是我见过的所有解决方案中最不烦人的。

于 2016-11-03T20:15:56.110 回答