-1

I'm starting learn Ruby on Rails, and started with a basic CRUD. A problem I'm having is in the index view: everything that I saved is showed, but also an array with everithing saved is returned.

The index method in my controller:

def index
  @categories = Category.all
end

My view:

<%= @categories.each do |c| %> 
<table>
  <tr>
    <td> <%= c.name %> </td>
  </tr>
</table>
<% end %>
<%= link_to 'New category', "/categories/new" %>

What is showed in the browser:

Healt
Supermarket
[#<Category id: 13, name: "Healt", created_at: "2016-02-17 13:30:22", updated_at: "2016-02-17 13:30:22">, #<Category id: 14, name: "Supermarket", created_at: "2016-02-17 13:30:42", updated_at: "2016-02-17 13:30:42">]New category

Why that happens? How can I avoid that?

rwehresmann
  • 1,108
  • 2
  • 11
  • 27

1 Answers1

2

This line has the = for output so it will (at the end) output the iteration (the array you did each on)

<%= @categories.each do |c| %> 

If you don't want that just remove the equals

<% @categories.each do |c| %> 
SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53