views:

31

answers:

2

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

+1  A: 

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

ipsum
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
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
A: 

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
Check this out it doesn't work, it still shows a list of %> on the page; <%# <td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete %></td> %>
AnApprentice
Thanks for the example, in the above, how would you comment out JUST the Destory line?
AnApprentice
I updated my post above, the comments are a bit confusing because you can see no newlines.
ipsum
Can you update your post to show how to comment out JUST the Destory line?
AnApprentice
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 %><td><% link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td><% 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