19

What is the Rails 3 way to comment out One Line or Multiple lines of code in a View? And so it doesn't show up in the HTML Source

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

5 Answers5

20

To comment out a single line ob ruby code use

<%# code %>
or for multiple lines
<%
=begin
 your code
=end
%>

EDIT: Here a example to comment out a loop in an view. The =begin and =end must stand directly at the beginning of the line. There couldn't be any spaces or tabs.

<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>
ipsum
  • 1,042
  • 7
  • 17
  • Check this out it doesn't work, it still shows a list of %> on the page; <%# <%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete %> %> – AnApprentice Sep 18 '10 at 16:46
  • Thanks for the example, in the above, how would you comment out JUST the Destory line? – AnApprentice Sep 18 '10 at 16:48
  • I updated my post above, the comments are a bit confusing because you can see no newlines. – ipsum Sep 18 '10 at 16:49
  • Can you update your post to show how to comment out JUST the Destory line? – AnApprentice Sep 18 '10 at 16:54
  • you can wrap this line with the =begin =end tags like in the edited example code. another (dirty) solution is to use a false condition e.g. <% if 1 == 2 %><% link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %><% end %> then you can write it in a single line. but be careful, with this method you can't directly see that the code is commented out. – ipsum Sep 18 '10 at 17:02
5

Rails 3 line comment within a view:

The f.label :name line has been commented out:

<%= form_for(@usr) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%#= f.label :name %>
  <%= f.text_field :name %>

  <%= f.submit "Save changes" %>
<% end %>

Because this is a view, the # must be within <% and %>.


Rails 3 multi-line comment within a view:

Begin multi-line comment:

<%
=begin
%>


End multi-line comment:

<%
=end
%>


Below, the entire form_for block has been commented out:

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


Do take note, for the multi-line comment tags to work, there can be no spaces or tabs in front of =begin or =end. They need to be at the very beginning of a line, or they will not work.

aMMT
  • 561
  • 5
  • 6
3

Here's what I do to hide comments from HTML (...whatever, it works!)

in your helpers/application.rb file:

def comment
    # use this keyword in the views, to comment-out stuff...
end

in your view:

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

what 'View Source' shows:

<h3></h3>

C-YA!

2

Some ways to comment code

<%
=begin
%>

RUBY CODE GOES HERE

<%
=end
%>

<% if false %>

RUBY CODE GOES HERE

<% end %>

<%# RUBY CODE%>
<%#= RUBY CODE%>

<!-- 
HTML CODE
-->

For RUBY code in.rb files like models/controllers

def xyz
  =begin
  blah blah code
  =end
end

For JS etc

/*
Some code
*/
1

which "blocks" do you mean? html? then you can use ruby code? <%# code %>

ipsum
  • 1,042
  • 7
  • 17
  • Sorry not blocks, just how to comment out a Line or LInes, in a non-HTML way. I don't want it in the HTML source – AnApprentice Sep 18 '10 at 16:31
  • Strange but that doesn't seem to always work... I get a compile error when using it in a view inside a loop like <% @projects.each do |project| %> – AnApprentice Sep 18 '10 at 16:42