Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - How to comment in a View?

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

like image 759
AnApprentice Avatar asked Sep 18 '10 16:09

AnApprentice


People also ask

How do you comment in rails?

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.

How do you comment multiple lines in rails?

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.


1 Answers

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>
like image 121
ipsum Avatar answered Oct 14 '22 03:10

ipsum