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
@users
, then i iterate over them in my view. I'm not sure how to properly assign an instance variable to individuals of a collection – pruett Mar 18 '12 at 16:31create.js.erb
being used? Presumably not from thefollowing
method in your controller, right? More likely it's part of acreate
method. So you need to use the instance variable that action is creating. – rfunduk Mar 18 '12 at 17:17