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

Fabian Pijpers
PLUS
Fabian Pijpers
Courses Plus Student 41,371 Points

Does anyone see where i mist the buck on this one?

I followed the examples but i get lost here?

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

<div id="pets">
  <h2>Pets</h2>
  <!-- YOUR CODE HERE -->
  <% @owner.pets.name do |comment| %>
    <%= render partial: "owner/pets", locals: {owner: pet} %>
  <% end %>
</div>
app/views/pets/_pet.html.erb
<div>
  <strong>Name:</strong>
  <%= render @owner.pet.name %>
</div>

2 Answers

AJ Tran
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
AJ Tran
Treehouse Teacher

Hi Fabian Pijpers ,

I've annotated your code block and hope that this will help you! Also, here is some really useful documentation about Using partial layouts

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

Line 1: comment is declared as a 'block variable' -- every step of the loop through owner's pet names will be called comment

Line 2: locals: {owner: pet} -- there are a few things to unpack here.

The key-value pairs owner and pet should match with the variable names in your partial and your loop

pet is being passed to your partial as a 'local variable' for the owner variable in owner/pets -- but pet has not been defined! Therefore, you may receive an undefined variable error when running this part of the app.

To fix this, start by making sure that both the variable names are the same. A convention I like to follow is to name my block variable so that it represents the collection I am looping over.

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

When we look at your second block, first we have to fix the syntax for the line <%= render @owner.pet.name %>:

<div>
  <strong>Name:</strong>
  <%= owner %>
</div>

The variable owner here will now correspond to the one in locals: {owner: pet_name}.

However, with these two changes combined, there is an inconsistency in names :) Look at my examples all together and see what I mean:

  <% @owner.pets.name do |pet_name| %>
    <%= render partial: "owner/pets", locals: {owner: pet_name} %>
  <% end %>
<div>
  <strong>Name:</strong>
  <%= owner %>
</div>

owner maybe isn't the best name to describe what data is being used -- right now you're just looping through pet_name. Here's a fun idea! If I change the first block to:

    <%= render partial: "owner/pets", locals: {name_of_pet: pet_name} %>

What would you have to change in your other block? :) I hope this helps you toward the right direction!

Fabian Pijpers
PLUS
Fabian Pijpers
Courses Plus Student 41,371 Points

Thanks for helping me in the right direction i was lost.