title: Ruby on Rails - Passing variables to a partial within a partial
body: <p>I am passing a collection (@feed_items) to a _feed_item partial via the :collection option. Inside the <strong>_feed_item</strong> partial I want to render another partial <strong>_like_button</strong>. In the <strong>_like_button</strong> partial I want to be able to access a specific member of the collection.  What should I be passing from the <strong>_feed_item</strong> partial to the <strong>_like_button</strong> partial?</p>
  
  <pre><code> @feed_items = current_user.user_feed.order("id DESC").limit(5)
  </code></pre>
  
  <p><strong>_feed.html.erb</strong></p>
  
  <pre><code> &lt;%= render partial: 'shared/feed_item', collection: @feed_items %&gt;
  </code></pre>
  
  <p><strong>_feed_item.html.erb</strong></p>
  
  <pre><code> &lt;%= render 'likes/like_button', ????? %&gt;
  </code></pre>
  
  <p><strong>_like_button.html.erb</strong></p>
  
  <pre><code> &lt;% if like = current_user.likes.find_by_feed_id(feed_item.id) %&gt;
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Passing the local variable as per the answers below worked:</p>
  
  <pre><code> &lt;%= render :partial =&gt; 'likes/like_button', :locals =&gt;{:feed_item =&gt; feed_item} %&gt;
  </code></pre>
  
  <p>However, after the like is submitted, I am getting a <code>ActionView::Template::Error (undefined local variable or method 'feed_item'</code> when it tries to render the form again through ajax. How can I pass the local variable again through the ajax call?</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>class LikesController &lt; ApplicationController
    def create
      @like = Like.create(params[:like])
      @feed = @like.feed
      render :toggle
    end
  
    def destroy
      like = Like.find(params[:id]).destroy
      @feed = like.feed
      render :toggle
    end
  end
  </code></pre>
  
  <p><strong>toggle.js.erb</strong></p>
  
  <pre><code> $("#like").html("&lt;%= escape_javascript render('like_button') %&gt;");
  </code></pre>
  
  <p><strong>_like_button.html.erb</strong></p>
  
  <pre><code> &lt;% if like = current_user.likes.find_by_feed_id(feed_item.id) %&gt;
    &lt;%= form_for like, :html =&gt; { :method =&gt; :delete }, :remote =&gt; true do |f| %&gt;
      &lt;%= f.submit "Unlike" %&gt;
    &lt;% end %&gt;
   &lt;% else %&gt;
    &lt;%= form_for current_user.likes.build(:feed_id =&gt; feed_item.id), :remote =&gt; true do |f| %&gt;
     &lt;%= f.hidden_field :feed_id %&gt;
     &lt;%= f.hidden_field :user_id %&gt;
     &lt;%= f.submit "Like" %&gt;
    &lt;% end %&gt;
   &lt;% end %&gt;
  </code></pre>
  
  <p><strong>_feed_item.html.erb</strong></p>
  
  <pre><code>    &lt;div id="like"&gt;
        &lt;%= render :partial =&gt; 'likes/like_button', :locals =&gt;{:feed_item =&gt; feed_item} %&gt;
      &lt;/div&gt;
  </code></pre>