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 Active Record Associations in Rails Using Associations in Your App Rendering Collections

Figmints Delicious Design
Figmints Delicious Design
14,305 Points

Render error

I thought that I was following the directions, however this throws an error message with a huge stack saying the the render is wrong, What am I missing here?

app/views/owners/show.html.erb
<h1>Owner: <%= @owner.name %></h1>

<div id="pets">
  <h2>Pets</h2>
  <!-- YOUR CODE HERE -->
  <%= @owner.pets.each do |name| %>
    <%= render partial: "pets/pet", locals: {name: name} %>
  <%= end %>

</div>
app/views/pets/_pet.html.erb
<div>
  <strong>Name:</strong>
  <%= pet.name %>
</div>

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

There are a couple issues here. Here's the first:

<%= end %>

You should only use <%= %> if you want to insert the result of an expression into the template output. Which you never want to do with an end keyword. Use <% %> here instead.

Once you solve that, this code will cause a different error:

  <%= @owner.pets.each do |name| %>
    <%= render partial: "pets/pet", locals: {name: name} %>
  <% end %>

The code @owner.pets.each do |name| causes each Pet to be stored, one at a time, in a variable called name. name doesn't contain a name, it contains a Pet. So you should probably change the name of your name variable to pet:

  <%= @owner.pets.each do |pet| %>
    <%= render partial: "pets/pet", locals: {pet: pet} %>
  <% end %>

Once you do that, this code in the partial (which is expecting a local variable named pet) should work correctly:

<div>
  <strong>Name:</strong>
  <%= pet.name %>
</div>