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

Stefan Piatkov
Stefan Piatkov
14,300 Points

Now we need to output HTML <p> elements with the name attribute of each pet. Within the each block, add <p></p> HTML tag

Not sure where I'm wrong.. If someone could please explain why I was wrong it'd be greatly appreciated.

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

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

The name you used for your each block's parameter doesn't match the name of the variable you're accessing within the each block:

<!-- The next line stores each pet in a variable called "name" -->
<% @pets.each do |name| %>
  <p>
    <!-- The next line accesses a nonexistent variable called "pet" -->
    <%= pet.name %>
  </p>
<% end %>

Change it to @pets.each do |pet|, and it should work.

If you haven't already done our Ruby Loops course, you may find some helpful info here.

Stefan Piatkov
Stefan Piatkov
14,300 Points

That didn't seem to work for me. I had a look back at previous videos and it just didn't make sense.

I passed the challenge by changing the parameter to title and the embedded Ruby to title.name though.. could it be a bug?

Jay McGavren
Jay McGavren
Treehouse Teacher

Stefan Piatkov I was able to pass the challenge using this code:

<% @pets.each do |pet| %>
  <p><%= pet.name %></p>
<% end %>
Stefan Piatkov
Stefan Piatkov
14,300 Points

I just tried it again, it worked. Thanks for your help Jay. :)