Paige Jones19,512 Points
Active Record render quiz question
As we set up in previous challenges, an Owner model has_many Pet instances. In the views/owners/show.html.erb template, an Owner object is available within the @owner instance variable. Using the template file and the views/pets/_pet.html.erb partial, render a view that includes the name attribute for each Pet belonging to @owner.
I've tried : <%= render @pets.name %> in the (views/owners/show.html.erb) and <%= pet.name %> in the (views/pets/_pet.html.erb)
but i get a major unreadable error. I think im still a little murky on exactly what im trying to do here. has anyone solved this problem?
<h1>Owner: <%= @owner.name %></h1> <div id="pets"> <h2>Pets</h2> <%= render @pets.name %> </div>
<div> <strong>Name:</strong> <%= # YOUR CODE HERE %> </div>
Steve HunterTreehouse Moderator 57,652 Points
I've not done this course so am unaware of the structure of the project or what previous tasks were about.
But, I had a go at this challenge and got an answer to pass. It doesn't use the partial, unfortunately, but I'm sure your prior knowledge of the course will let you implement the usage of that file.
I created an
each loop in the view. In this, I took the
@owner instance and looped over
each element in the
pets property. I then output the
name attribute of the loop variable, which I called
p, because I lack imagination.
That all looks like:
<h1>Owner: <%= @owner.name %></h1> <div id="pets"> <h2>Pets</h2> <!-- YOUR CODE HERE --> <% @owner.pets.each do |p| %> <%= p.name %> <% end %> </div>
I hope that helps you out a little.
Seth Kroger56,351 Points
It should be
<%= render @owner.pets %> in the (views/owners/show.html.erb) because pets doesn't exist on its own. It is a property of
Gabriel Ochoa11,698 Points
I wanted to try and explain this because I am not entirely sure if I get it 100% but hopefully it helps someone. I think the answer to this question is correct, but it is not using the partial method that the original question is asking.
Basically the question wants us to understand that it is good practice to use partial views to make the code more readable.
The output of the code should be the owner's pet's name.
The partial view is views/pets/_pet.html.erb_ You can tell it is a partial view by the underscore before pet.html.erb
<%= render @owner.pets %>
The line above is a short cut way of rendering the partial view, the way I think someone should look at the problem is to ask themselves; how does the line above actually execute? How does it know where to look for the partial view?
The answer is because rails takes the @owner.pets and constructs the directory of where the partial layout is from the .pets creates-> pets/_pet.html.erb. So the first part is done, you have hooked up a partial view to a view.
Note that within the render @owner.pets, it is also making a local variable pet, which is used in the partial view and necessary for the second part of this question.
The second part of the question asks to display the pets name's through the partial view. Like I stated above, the local variable is pet which is the collection of pet names that belong to the owner, therefore you can simply call the method name and it will display all of the dogs names that belong to the owner.
<%= pet.name %> would go into the partial view.
Hopefully this help someone out a little bit. I think the video prior to this challenge goes through this, but it is a little too fast, and hard to grasp. Not sure if I've done any better.