I have a deeply nested partial view, where I iterate over a collection of @users. The controller defines the @users like so:
class UsersController < ApplicationController
def following
@users = @user.followed_users
..
end
In the partial templates, I iterate over the collection, but I never define the individual of a collection as an instance variable, instead I pass the local variables along with :object:
<% if current_user.following?(user) %>
<%= render partial: 'unfollow', object: user, as: :user %>
<% else %>
<%= render partial: 'follow', object: user, as: :user %>
<% end %>
Problem is…
Inside the create.js.erb file that handles the Follow action, for example, I use the following code:
$('#follow_form').html("<%=j render partial: 'users/unfollow', object: user, as: :user %>");
This gives me the following errors from my dev log:
ActionView::Template::Error (undefined local variable or method `user’ for #<#:0x007fa17b9e57e0>):
Am I declaring the partial wrong in jQuery? Do I have to define user somewhere else, like in the controller? If so how do I define an individual of a collection as an instance variable?
Thanks in advance!
EDIT: Included associated controller
Here is the associated controller that handles the request:
class RelationshipsController < ApplicationController
def create
debugger
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
@Dipil helped me out with the simple solution of replacing the local variable
userwith the instance variable@userset in the controller. This worked perfectly.Final output after some minor refactoring: