Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Rails Routes and Resources A Route to an Index Action Populating the View

We need to populate our page with the list of pets. For this first task, use the each method in ERB tags to loop over ea

Can I get some help? I can't seem to answer this question. Thanks

app/views/pets/index.html.erb
<% @pets.each do |pet| %>
  <p> <%= pet.all %></p>
<% end %>

3 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

You're close! Now that you've set up your each loop, the pet variable contains the current Pet object. The problem is that you're calling a method named all on the Pet object, but the Pet class doesn't have an all method. Instead, you need to call the name method on the Pet object.

martinaraignee
martinaraignee
12,786 Points
<% pets.each do |pet| %>
  <p><%= pets.name %></p>
<% end %>

does not accept. Could you please explain why not

Jay McGavren
Jay McGavren
Treehouse Teacher

martinaraignee pets is an array of Pet objects. The array does not have a name method. The Pet objects within the array each have a name method, though. The each method assigns each Pet object to the pet variable, one at a time. So you need to call pet.name, not pets.name.

It's important to understand how the each method works before you go on. Make sure you've viewed the course's prerequisites, especially our Ruby Loops course.

martinaraignee
martinaraignee
12,786 Points

Couldn't ask for better explanation. Thank you

Muhammad sharifi
Muhammad sharifi
4,455 Points

this works <% @pets.each do |pet| %> <p><%= pet.name %> </p> <% end %> please mark as the accepted answer.