PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] How do I pass a collection from a parent partial to an arbitrary sub-partial depth?

 May 18, 2022     partial, ruby-on-rails     No comments   

Issue

I'm following the RailsTutorial, and I'm currently stuck on exercise 10.5.5. Since I'm changing quite a bit, I've put the code into a paste (updated).

There are a few things to note before going into the paste:

  1. One of the original partials receives a collection from it's "parent" partial, while the other receives the object directly from the controller.
  2. The if statement in both of these "child" partials uses a different object name, but they're represent the same object in the database.

Ideally, I'd like to move the if statement into the grandchild, or sub-sub-, partial. I can leave it in the child partial if need be, but this doesn't feel DRY.

I've tried rendering the grandchild partial with <%= render partial: 'shared/foo', object: bar, as: :baz %> so I can use baz in the grandchild partial, since the other child partial uses baz by default. In that partial, I'm just doing <%= render partial: 'shared/foo', object: baz %>. Confused? Me, too.

You'll notice I've tried rendering the partials both with and without passing in the parent object. Maybe the parent object needs to be redefine? I also just tried <%= render partial: 'shared/micropost_delete_link', object: feed_item %>, but no luck.

Each approach I've tried so far yields the same error in the tests:

 Failure/Error: before { visit root_path }
 ActionView::Template::Error:
   undefined method `user' for :feed_item:Symbol

This seems to indicate that I can't pass a single object received from the parent option collection: @feed_items.

Update: There was a typo in my original paste. With that fixed in the updated paste, my tests are still failing.

 Failure/Error: before { visit root_path }
 ActionView::Template::Error:
   undefined method `user' for nil:NilClass

Solution

Somewhere along the line, I tried a different syntax, and the tests started passing:

<%= render partial: 'shared/micropost_delete_link', locals: { micropost: feed_item } %>

Even though the docs say the following should be equivalent:

<%= render :partial => "account", :object => @buyer, :as => 'user' %>
<%= render :partial => "account", :locals => { :user => @buyer } %>

Testing is still a bit unusual for me, so I can't rule out that it forced something in the suite to be re-evaluated.



Answered By - jrhorn424
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing