Reputation: 191
I'm trying to allow users to favorite posts and then it show them sort of of interaction through AJAX, but it's not working.
The error I'm getting in the console is:
ActionView::Template::Error (undefined local variable or method `post_item' for #<#<Class:0x007fecb2a3d5f8>:0x007fecb2a357e0>):
The button is being rendered through a partial:
<%= render "shared/fave_form", post_item: post_item %>
Here's the code for the button (shared/_fave_form.html.erb):
<% if current_user.voted_on?(Post.find(post_item)) %>
<%= link_to "unlike", vote_against_post_path(post_item.id), :remote => true, :method => :post, :class => "btn") %>
<% else %>
<%= link_to "like", vote_up_post_path(post_item.id), :remote => true, :method => :post, :class => "btn") %>
<% end %>
Here's the toggle.js.erb file:
$("#fave").html("<%= escape_javascript render('fave_form') %>");
Upvotes: 0
Views: 142
Reputation: 47522
When you render the partial using toggle.js.erb
it is not getting locals
value post_item
, you have to provide it in also.So, your js code should be something like following
$("#fave").html("<%= escape_javascript(render :partial=>"fave_form", locals: {post_item: post_item}).html_safe %>);
I guess you are using some ajax call and then your toggle.js.erb
so in your toggle
action you must specify value to post_item
, lets make it instance variable @post_item
so that we can use it in toggle.js.erb
.
$("#fave").html("<%= escape_javascript(render :partial=>"fave_form", locals: {post_item: @post_item}).html_safe %>);
Upvotes: 1
Reputation: 6682
The partial is using a local variable, so pass post_item
as a local:
<%= render :partial => "shared/fave_form", :locals => {post_item: post_item} %>
Upvotes: 0