Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Stefan Piatkov
Stefan Piatkov
14,312 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,312 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,312 Points

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