如何在rails html中注释行。erb文件?(复制)

时间:2023-01-24 11:32:39

This question already has an answer here:

这个问题已经有了答案:

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文件。

3 个解决方案

#1


165  

ruby on rails notes has a very nice blogpost about commenting in erb-files

ruby on rails notes有一篇关于在esb -files中进行评论的很好的博文

the short version is

简短的版本是

to comment a single line use

注释单行使用

<%-# commented line -%>

to comment a whole block use a if false to surrond your code like this

要对整个块进行注释,请使用if false环绕您的代码,如下所示

<% if false %>
code to comment
<% end %>

#2


24  

Note that if you want to comment out a single line of printing erb you should do like this

注意,如果您想注释掉一行打印erb,您应该这样做

<%#= ["Buck", "Papandreou"].join(" you ") %>

#3


2  

This is CLEANEST, SIMPLEST ANSWER for CONTIGUOUS NON-PRINTING Ruby Code:

The below also happens to answer the Original Poster's question without, the "ugly" conditional code that some commenters have mentioned.

下面也恰好回答了最初的海报的问题,没有一些评论者提到的“丑陋”的条件代码。


  1. CONTIGUOUS NON-PRINTING Ruby Code

    连续打印Ruby代码

    • This will work in any mixed language Rails View file, e.g, *.html.erb, *.js.erb, *.rhtml, etc.

      这将适用于任何混合语言的Rails视图文件。g * . html。erb,* . js。* erb。rhtml等等。

    • This should also work with STD OUT/printing code, e.g. <%#= f.label :title %>

      这也应该适用于STD输出/打印代码,例如<%#= f。标签:标题% >

    • DETAILS:

      细节:

      Rather than use rails brackets on each line and commenting in front of each starting bracket as we usually do like this:

      不要像我们通常做的那样,在每一行上使用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 %>
      

      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:

      如果你把你的代码写为一个大的块,你可以在第一个打开的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 
        %>
      

#1


165  

ruby on rails notes has a very nice blogpost about commenting in erb-files

ruby on rails notes有一篇关于在esb -files中进行评论的很好的博文

the short version is

简短的版本是

to comment a single line use

注释单行使用

<%-# commented line -%>

to comment a whole block use a if false to surrond your code like this

要对整个块进行注释,请使用if false环绕您的代码,如下所示

<% if false %>
code to comment
<% end %>

#2


24  

Note that if you want to comment out a single line of printing erb you should do like this

注意,如果您想注释掉一行打印erb,您应该这样做

<%#= ["Buck", "Papandreou"].join(" you ") %>

#3


2  

This is CLEANEST, SIMPLEST ANSWER for CONTIGUOUS NON-PRINTING Ruby Code:

The below also happens to answer the Original Poster's question without, the "ugly" conditional code that some commenters have mentioned.

下面也恰好回答了最初的海报的问题,没有一些评论者提到的“丑陋”的条件代码。


  1. CONTIGUOUS NON-PRINTING Ruby Code

    连续打印Ruby代码

    • This will work in any mixed language Rails View file, e.g, *.html.erb, *.js.erb, *.rhtml, etc.

      这将适用于任何混合语言的Rails视图文件。g * . html。erb,* . js。* erb。rhtml等等。

    • This should also work with STD OUT/printing code, e.g. <%#= f.label :title %>

      这也应该适用于STD输出/打印代码,例如<%#= f。标签:标题% >

    • DETAILS:

      细节:

      Rather than use rails brackets on each line and commenting in front of each starting bracket as we usually do like this:

      不要像我们通常做的那样,在每一行上使用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 %>
      

      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:

      如果你把你的代码写为一个大的块,你可以在第一个打开的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 
        %>