9

I am passing a collection (@feed_items) to a _feed_item partial via the :collection option. Inside the _feed_item partial I want to render another partial _like_button. In the _like_button partial I want to be able to access a specific member of the collection. What should I be passing from the _feed_item partial to the _like_button partial?

 @feed_items = current_user.user_feed.order("id DESC").limit(5)

_feed.html.erb

 <%= render partial: 'shared/feed_item', collection: @feed_items %>

_feed_item.html.erb

 <%= render 'likes/like_button', ????? %>

_like_button.html.erb

 <% if like = current_user.likes.find_by_feed_id(feed_item.id) %>

EDIT

Passing the local variable as per the answers below worked:

 <%= render :partial => 'likes/like_button', :locals =>{:feed_item => feed_item} %>

However, after the like is submitted, I am getting a ActionView::Template::Error (undefined local variable or method 'feed_item' when it tries to render the form again through ajax. How can I pass the local variable again through the ajax call?

Controller

class LikesController < 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

toggle.js.erb

 $("#like").html("<%= escape_javascript render('like_button') %>");

_like_button.html.erb

 <% if like = current_user.likes.find_by_feed_id(feed_item.id) %>
  <%= form_for like, :html => { :method => :delete }, :remote => true do |f| %>
    <%= f.submit "Unlike" %>
  <% end %>
 <% else %>
  <%= form_for current_user.likes.build(:feed_id => feed_item.id), :remote => true do |f| %>
   <%= f.hidden_field :feed_id %>
   <%= f.hidden_field :user_id %>
   <%= f.submit "Like" %>
  <% end %>
 <% end %>

_feed_item.html.erb

    <div id="like">
      <%= render :partial => 'likes/like_button', :locals =>{:feed_item => feed_item} %>
    </div>

4 Answers 4

8
 <%= render :partial => 'likes/like_button', :locals =>{:feed_item => feed_item} %>

Rails has got some awesome documention http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables

In toggle.js

 $("#like").html("<%= escape_javascript(render :partial => 'likes/like_button', :locals =>{:feed_item => @feed}) %>");
6
  • Thank you. That works (the database is updating), however I am still getting an error in the console ActionView::Template::Error (undefined local variable or method 'feed_item' for #<#<Class:0x00000102f6b078>:0x0000010452f7d8>). Any idea what might be causing this?
    – diasks2
    Sep 4, 2012 at 8:15
  • add the whole trace check which file it has been traversing through Sep 4, 2012 at 8:25
  • may be you are calling it from any other form and the locals needs to be passed from there as well Sep 4, 2012 at 8:27
  • The error is actually happening when it is rendering the form again through the ajax call. Rendered likes/_like_button.html.erb (17.3ms) Rendered likes/create.js.erb (18.4ms) Completed 500 Internal Server Error in 30ms ActionView::Template::Error (undefined local variable or method 'feed_item' for #<#<Class:0x000001046b46f8>:0x00000105c71f68>): 1: <% if like = current_user.likes.find_by_feed_id(feed_item.id) %> 2: <%= form_for like, :html => { :method => :delete }, :remote => true do |f| %> 3: <%= f.submit "Unlike" %> 4: <% end %>
    – diasks2
    Sep 4, 2012 at 8:30
  • This is my JavaScript Embedded Ruby file. How can I pass the local variable to this as well? $("#like").html("<%= escape_javascript render('like_button') %>");
    – diasks2
    Sep 4, 2012 at 8:42
1

You need to pass the feed_item variable as a local to the _like_button.html.erb partial.

<%= render 'likes/like_button', :locals => { :feed_item => feed_item } %>

This just sets the local named feed_item in the inner partial to be whatever feed_item is in the outer partial.

2
  • Thank you. That works (the database is updating), however I am still getting an error in the console ActionView::Template::Error (undefined local variable or method 'feed_item' for #<#<Class:0x00000102f6b078>:0x0000010452f7d8>). Any idea what might be causing this?
    – diasks2
    Sep 4, 2012 at 8:16
  • create feed_item as an instance variable (@feed_item) and then use it in the inner partial with @feed_item instead of feed_item to avoid the error. Jan 21, 2014 at 12:17
1

When using collection to render the partial, so you wil be getting the local variable same as the render file name. So just you can pass the file name as a local variable.

For example if you have _feed_item.html.erb and you are rendering this partial using collection like

render "feed_item", collection: @feed_items

So you will be getting the feed_item local variable inside this feed_item partial.

So you can pass this feed_item variable as a local variable to another partial(in your case _like_button.html.erb)

2
  • Thank you. That works (the database is updating), however I am still getting an error in the console ActionView::Template::Error (undefined local variable or method 'feed_item' for #<#<Class:0x00000102f6b078>:0x0000010452f7d8>). Any idea what might be causing this?
    – diasks2
    Sep 4, 2012 at 8:16
  • where you give this "feed_item", can i know this?
    – Mohanraj
    Sep 4, 2012 at 9:20
0

If I understand, you have to do the following:

_feed.html.erb

 <%= render partial: 'shared/feed_item', collection: @feed_items %>

_feed_item.html.erb

Here you are defining a local variable called feed_item and assigment the value as each element of your collection. You can omit the word locals because Rails is smart enough to know that is an a local variable.

 <% @feed_items.each do |feed_item| %>
 <%= render 'likes/like_button', feed_item: feed_item %>
 <% end %>

_like_button.html.erb

 <% if like = current_user.likes.find_by_feed_id(feed_item.id) %>

Hope it helps you.

1
  • Thanks for your help. I am now having trouble rendering the form after an ajax update (see my edits to the question above). Do you know what might be going wrong?
    – diasks2
    Sep 4, 2012 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.