Ruby Active Record Associations in Rails Using Associations in Your App Rendering Collections

Sam Ruedinger
Sam Ruedinger
6,862 Points

Is this a problem with Workspaces or am I just more confused than I thought?

I am getting the following error when I try to complete this code challenge. It looks like it's referring to a whole lot of directories and files that I'm not using for this challenge and I'm thinking it's a Workspaces problem, but I'm not certain. Can anyone confirm?

There seems to be an error in your code: /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:310:in module_eval': /workdir/app/views/owners/show.html.erb:5: syntax error, unexpected ',', expecting => (SyntaxError) ...ial: "pets/pets", locals: {pet, pet} );@output_buffer.safe_a... ... ^ /workdir/app/views/owners/show.html.erb:6: syntax error, unexpected keyword_end, expecting ')' '.freeze; end ^ /workdir/app/views/owners/show.html.erb:9: syntax error, unexpected keyword_ensure, expecting ')' /workdir/app/views/owners/show.html.erb:11: syntax error, unexpected keyword_end, expecting ')' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:310:incompile' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:259:in block (2 levels) in compile!' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications.rb:166:ininstrument' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:356:in instrument' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:258:inblock in compile!' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:246:in synchronize' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:246:incompile!' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:158:in block in render' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications.rb:166:ininstrument' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:354:in instrument' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/template.rb:157:inrender' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/template_renderer.rb:54:in block (2 levels) in render_template' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/abstract_renderer.rb:42:inblock in instrument' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications.rb:164:in block in instrument' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications/instrumenter.rb:21:ininstrument' from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications.rb:164:in instrument' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/abstract_renderer.rb:41:ininstrument' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/template_renderer.rb:53:in block in render_template' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/template_renderer.rb:61:inrender_with_layout' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/template_renderer.rb:52:in render_template' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/template_renderer.rb:14:inrender' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/renderer.rb:42:in render_template' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/renderer/renderer.rb:23:inrender' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/rendering.rb:103:in _render_template' from /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/streaming.rb:217:in_render_template' from /usr/local/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/rendering.rb:83:in render_to_body' from /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/rendering.rb:52:inrender_to_body' from /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/renderers.rb:142:in render_to_body' from /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/abstract_controller/rendering.rb:48:inrender_to_string' from /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/rendering.rb:41:in render_to_string' from /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/renderer.rb:80:inrender' from /usr/local/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal/rendering.rb:11:in `render'

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

<div id="pets">
  <h2>Pets</h2>
    <%= render partial: "pets/pets", locals: {pet, pet} %>
    <% end %>
</div>
app/views/pets/_pet.html.erb
<div>
  <strong>Name:</strong>
  <%= # YOUR CODE HERE %>
</div>
Chanda Lupambo
Chanda Lupambo
Pro Student 22,208 Points

Your error in your is you're render _pets not _pet change it to:

<%= render partial: "pets/pet", locals: {pet, pet} %>

2 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

The error you got is referring to the fact that locals needs to be a hash with the local variables you want to set as its keys. The corresponding value for each key should be the value you want the local variable to have. {pet, pet} is not valid syntax for a hash, but {pet: pet} would be.

If you fix the above issue, you'll get a new error. You need to loop through each of the values in @owner.pets, like this:

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

Right now, you only have the <% end %> keyword for the each block, which will give you another error.

There are also easier ways to do the same thing. I would recommend reviewing the teacher's notes on this video, then going back and re-solving this challenge using each of the three techniques shown there. The challenge will accept all three ways of solving it, and you'll need to know all three ways eventually!

Sam Ruedinger
Sam Ruedinger
6,862 Points

Appreciate the response. I actually thought it was pet, singular, when I originally tried the challenge, but I got the error text so I was trying to play around with variations. I retried it now as you wrote it and still got the long book of errors. Good to know I was originally right though. :-)