Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
    
Yes, it seems the user variable is not defined. In the controller code where you follow the user, assign that user to a @user variable, then use that instead of user in create.js.erb –  Dipil Mar 18 '12 at 6:59
    
@Dipil in the controller, i have a variable @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:31
    
can you add the backtrace for the exception so the error can be traced back.. –  Dipil Mar 18 '12 at 17:15
    
Where is create.js.erb being used? Presumably not from the following method in your controller, right? More likely it's part of a create method. So you need to use the instance variable that action is creating. –  rfunduk Mar 18 '12 at 17:17
1  
pruett the create.js.erb view only has access to the variables defined in the create action. So, I think simply using @user instead of user in create.js.erb will solve your problem here. –  Dipil Mar 18 '12 at 17:43
show 3 more comments

1 Answer

up vote 0 down vote accepted

@Dipil helped me out with the simple solution of replacing the local variable user with the instance variable @user set in the controller. This worked perfectly.

Final output after some minor refactoring:

$("#follow_form").html("<%=j render partial: 'users/unfollow', locals: { user: @user } %>");
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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