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
The Ruby single-line comment begins with the # character and ends at the end of the line. Any characters from the # character to the end of the line are completely ignored by the Ruby interpreter. The # character doesn't necessarily have to occur at the beginning of the line; it can occur anywhere.
Despite the existence of =begin and =end , the normal and a more correct way to comment is to use # 's on each line. If you read the source of any ruby library, you will see that this is the way multi-line comments are done in almost all cases.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With