Vladislav Vladislav - 6 months ago 13
Ruby Question

How to hide database records from view?

I'm beginner in Ruby On Rails and I'm reading "Getting Started with Rails" now. I made models and controllers for Article and Comments, everything ok, I'm able to add comments and they appear on Article view, but besides I see records from database in this format: [# Comment id: 1, commenter ... updated_at: "2016-05-20 09:26:25"]. Why they appear and how to hide it? Code of Article's view show.html.erb:

<p>
<strong>Title:</strong>
<%= @article.title %>
</p>

<p>
<strong>Text:</strong>
<%= @article.text %>
</p>

<h2>Comments</h2>
<%= @article.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>>
<%= comment.body %>
</p>
<% end %>

<h3>Add a comment</h3>
<%= form_for([@article, @article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>

<%= link_to 'Edit', edit_article_path(@article) %>
<%= link_to 'Back', articles_path %>

Answer

Instead of:

<%= @article.comments.each do |comment| %>

It should be:

<% @article.comments.each do |comment| %>

<%= %> used to render something. And you are iterating an object so you need to use <% %>.

Comments