Block comments in html.erb templates in rails

前端 未结 16 2159
北荒
北荒 2020-12-07 09:10

How do you comment out html mixed with ruby code?

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

In js

相关标签:
16条回答
  • 2020-12-07 09:47

    Since you can use <% %> to put a ruby block, it can be certainly used to put in comments into it.

    A simpler and elegant solution would look like...

    <%
    # 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?
    %>
    
    0 讨论(0)
  • 2020-12-07 09:47

    You can use both <%if false%> and HTML comments at the same time:

    <%if false%><--
    
    stuff to comment out
    
    --><%end%>
    

    The benefits are:

    • Ruby code is not executed

    • The commented block has gray color in IDE

    • The intention is obvious for other developers

    0 讨论(0)
  • 2020-12-07 09:48

    After =begin you do not need to put %>

    <%
    =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 %>
    
    0 讨论(0)
  • 2020-12-07 09:50

    Just an addendum to some of the previous answers. I found the =begin/=end solution most useful, but for the sake of beauty I write it like so:

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

    Note that since everything is ignored until the =end there is no need to close the =begin tag with %> or open the =end tag with <% (which has also been pointed out in an earlier answer)

    I found this to be the most elegant solution to completely outcomment a block of mixed ruby and html code and have it greyed out in my editor as well, as opposed to the <% if false %> solution. Only drawback is that =begin and =end must be placed at the very beginning of the line..

    0 讨论(0)
  • 2020-12-07 09:56

    Use this for commenting single lines:

    <%# your_ruby_code %>
    

    For multiple lines, the

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

    What you said would work.

    0 讨论(0)
  • 2020-12-07 09:58

    You have to bear in mind where the code is executed. Ruby-style comments work because the Ruby code is executed on the server before it is served to the web browser. This also explains why HTML comments do not work—the Ruby has already been executed.

    Doesn't the IDE you're using support creating custom macros for commenting out blocks of code?

    0 讨论(0)
提交回复
热议问题