title: Rails 3 Nested Partials, Collections, Locals (Rails Tutorial 2nd Ed : Chapter 10, Exercise 5)
body: <p>Working through <a href="http://ruby.railstutorial.org/chapters/user-microposts?version=3.2#sec%3amicropost_exercises" rel="nofollow">Michael Hartl's Rails Tutorial 2nd Ed Chapter 10 Exercise 5</a>, I've run into problem with partials and collections and using a partial within a partial.</p>
  
  <p>Chapter 10, Exercise 5 states:
  <strong>"Using partials, eliminate the duplication in the delete links from Listing 10.46 and Listing 10.47."</strong></p>
  
  <p>Listing 10.46 : app/views/microposts/_micropost.html.erb</p>
  
  <pre><code>&lt;li&gt;
    &lt;span class="content"&gt;&lt;%= micropost.content %&gt;&lt;/span&gt;
    &lt;span class="timestamp"&gt;
      Posted &lt;%= time_ago_in_words(micropost.created_at) %&gt; ago.
    &lt;/span&gt;
    &lt;% if current_user?(micropost.user) %&gt;
      &lt;%= link_to "delete", micropost, method:  :delete,
                                       confirm: "You sure?",
                                       title:   micropost.content %&gt;
    &lt;% end %&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>Listing 10.47 : app/views/shared/_feed_item.html.erb</p>
  
  <pre><code>&lt;li id="&lt;%= feed_item.id %&gt;"&gt;
    &lt;%= link_to gravatar_for(feed_item.user), feed_item.user %&gt;
      &lt;span class="user"&gt;
        &lt;%= link_to feed_item.user.name, feed_item.user %&gt;
      &lt;/span&gt;
      &lt;span class="content"&gt;&lt;%= feed_item.content %&gt;&lt;/span&gt;
      &lt;span class="timestamp"&gt;
        Posted &lt;%= time_ago_in_words(feed_item.created_at) %&gt; ago.
      &lt;/span&gt;
    &lt;% if current_user?(feed_item.user) %&gt;
      &lt;%= link_to "delete", feed_item, method:  :delete,
                                       confirm: "You sure?",
                                       title:   feed_item.content %&gt;
    &lt;% end %&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>My approach was to create this file
  shared/_item_delete_link.html.erb</p>
  
  <pre><code>&lt;%= link_to "delete", item, method:  :delete,
                                   confirm: "You sure?",
                                   title:   item.content %&gt;
  </code></pre>
  
  <p>Then use this partial in the original partials like this:</p>
  
  <p>Listing 10.46 : app/views/microposts/_micropost.html.erb</p>
  
  <pre><code>&lt;li&gt;
      &lt;span class="content"&gt;&lt;%= micropost.content %&gt;&lt;/span&gt;
      &lt;span class="timestamp"&gt;
          Posted &lt;%= time_ago_in_words(micropost.created_at) %&gt; ago.
      &lt;/span&gt;
      &lt;% if current_user?(micropost.user) %&gt;
    &lt;%= render partial: 'shared/item_delete_link', collection: @microposts, as: :item %&gt;
      &lt;% end %&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>Listing 10.47 : app/views/shared/_feed_item.html.erb</p>
  
  <pre><code>&lt;li id="&lt;%= feed_item.id %&gt;"&gt;
    &lt;%= link_to gravatar_for(feed_item.user), feed_item.user %&gt;
    &lt;span class="user"&gt;
      &lt;%= link_to feed_item.user.name, feed_item.user %&gt;
    &lt;/span&gt;
    &lt;span class="content"&gt;&lt;%= feed_item.content %&gt;&lt;/span&gt;
    &lt;span class="timestamp"&gt;
      Posted &lt;%= time_ago_in_words(feed_item.created_at) %&gt; ago.
    &lt;/span&gt;
    &lt;% if current_user?(feed_item.user) %&gt;
    &lt;%= render partial: 'shared/item_delete_link', collection: @feed_items, as: :item %&gt;
    &lt;% end %&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>This got all my tests to pass, so I thought it was working until I checked it in the browser:
  <a href="http://grab.by/daUk" rel="nofollow">http://grab.by/daUk</a></p>
  
  <p>The entire collection is being rendered again for each of the _item_delete_link partials, whereas what I wanted was to pass through the local variable from the original collection used in the parent partial.</p>
  
  <p>I tried using the <code>locals: { }</code> and the <code>object:</code> options for render, but had no luck.</p>
  
  <p>Anyone know the answer? Thanks!</p>