【问题标题】:Rails 3 - How to comment in a View?Rails 3 - 如何在视图中评论?
【发布时间】:2010-09-18 16:24:07
【问题描述】:

Rails 3 在视图中注释一行或多行代码的方法是什么?所以它不会出现在 HTML 源代码中

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    要注释掉一行 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>
    

    【讨论】:

    • 检查一下它不起作用,它仍然在页面上显示 %> 的列表; 'Are you sure?', :method => :delete %> %>
    • 感谢您的示例,在上面,您将如何注释掉 JUST Destory 行?
    • 我更新了上面的帖子,cmets 有点混乱,因为你看不到换行符。
    • 您能否更新您的帖子以显示如何注释掉 JUST the Destory 行?
    • 您可以使用 =begin =end 标签包裹这一行,就像在编辑的示例代码中一样。另一个(肮脏的)解决方案是使用错误的条件,例如 'Are you sure?', :method => :delete %> then您可以将其写在一行中。但要注意,用这种方法是不能直接看到代码被注释掉的。
    【解决方案2】:

    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 前面不能有空格或制表符。它们必须位于一行的开头,否则它们将不起作用。

    【讨论】:

      【解决方案3】:

      这是我从 HTML 中隐藏 cmets 的方法(...无论如何,它都有效!)

      在您的 helpers/application.rb 文件中:

      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!

      【讨论】:

      • OOH.. 刚刚注意到 OP 提出这个问题的日期。我真的希望他不是在等我解决这个问题!
      【解决方案4】:

      注释代码的一些方法

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

      【讨论】:

        【解决方案5】:

        您指的是哪些“块”? html?那么你可以使用 红宝石代码?

        【讨论】:

        • 抱歉没有阻塞,只是如何以非 HTML 方式注释掉一行或多行。我不希望它出现在 HTML 源代码中
        • 奇怪,但这似乎并不总是有效...在像
        猜你喜欢
        • 2010-12-19
        • 2013-02-26
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多