# instance variable, ajax and partial

**URL:** <https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219>\
**Category:** rubyonrails-talk\
**Created:** [April 18, 2008, 10:13pm UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219 "2008-04-18T22:13:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [April 18, 2008, 10:13pm UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/1 "2008-04-18T22:13:23Z")

</div>

i get an error message 'you might have expexted an instance of Array, error occurred while evaluating nil

i have created @selected\_team = Array.new in a previous def that has been called. if i put @selected\_team in the def below, it works but it also zeros all the data each time it is called. am i missing something basic here. arn't instance variable available thru all def in a controller as well as the views? the instance variable is working in the view via partial!

def roster\_selection &nbsp;&nbsp;&nbsp;&nbsp;if params[:cvalue] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@selected\_team \<\< [params[:full\_name], params[:user\_id] ] &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# take out name &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;render :partial =\> "selected" &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 18, 2008, 10:54pm UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/2 "2008-04-18T22:54:08Z")

</div>

> i get an error message 'you might have expexted an instance of Array, error occurred while evaluating nil
> 
> i have created @selected\_team = Array.new in a previous def that has been called. if i put @selected\_team in the def below, it works but it also zeros all the data each time it is called. am i missing something basic here. arn't instance variable available thru all def in a controller as well as the views? the instance variable is working in the view via partial!

I think you're going to have to show us a little more than this.

Fred

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [April 18, 2008, 11:59pm UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/3 "2008-04-18T23:59:58Z")

</div>

def show &nbsp;&nbsp;&nbsp;&nbsp;@club = Club.find params[:id] &nbsp;&nbsp;&nbsp;&nbsp;rosternew(@club.id, params[:sort\_by] ) &nbsp;&nbsp;&nbsp;&nbsp;@selected\_team = Array.new # @selected\_array variable. &nbsp;&nbsp;end

everything works well. i have a onclick on a checkbox, when clicked it calls roster\_selection, the name and the user\_id is passed and i can see them in the logs. but i get a nil error when @selected\_array \<\< [x , y] as in the code.

if i directly put @selected = Array.new in the def called everything runs but of course my array is initialized and contains only one name. let me know if there is something specific you need to see. i have even tried rjs, but i have now narrowed that out as well because the error is the same. i can not fill this array for some reason.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 19, 2008, 12:09am UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/4 "2008-04-19T00:09:20Z")

</div>

> def show &nbsp;&nbsp;&nbsp;@club = Club.find params[:id] &nbsp;&nbsp;&nbsp;rosternew(@club.id, params[:sort\_by] ) &nbsp;&nbsp;&nbsp;@selected\_team = Array.new # @selected\_array variable. end
> 
> everything works well. i have a onclick on a checkbox, when clicked it calls roster\_selection, the name and the user\_id is passed and i can see them in the logs. but i get a nil error when @selected\_array \<\< [x , y] as in the code.

That's because each action is handled by a separate controller  
instance (which could be in a separate mongrel, or even on a different  
machine if your app is deployed across multiple machines). setting an  
instance variable in your show action has no effect on whether what  
exists when your roster\_selection action is called.

Fred

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [April 19, 2008, 12:36am UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/5 "2008-04-19T00:36:41Z")

</div>

> That's because each action is handled by a separate controller instance (which could be in a separate mongrel, or even on a different machine if your app is deployed across multiple machines). setting an instance variable in your show action has no effect on whether what exists when your roster\_selection action is called.

i read this a couple of times, both def are in the same controller. both actions as you refer to it.

i even tried this inside the view \<%= @team \<\< ['hello', 'goodbye'] same error message. i am going to look a little bit closer at the local variable in partials. i have a feeling it has something to do with that.

this is a trivial problem so i can't believe that it can not be done in ruby using javascript/ajax.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 19, 2008, 12:39am UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/6 "2008-04-19T00:39:49Z")

</div>

> > That's because each action is handled by a separate controller instance (which could be in a separate mongrel, or even on a  
> > different machine if your app is deployed across multiple machines). setting an instance variable in your show action has no effect on whether what exists when your roster\_selection action is called.
> 
> i read this a couple of times, both def are in the same controller. both actions as you refer to it.

The code runs in response to separate http requests (unless i've  
completely misunderstood your requests). Instance variables are not  
preserved across requests.

Fred

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [April 19, 2008, 12:56am UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/7 "2008-04-19T00:56:21Z")

</div>

fredirick, i don't think i have explained my self. i have used instance variables across def without a problems. this has something to do with the call from javascript. i wanted the user to be able to click on a check box and that item appear in a div (and i need to keep track of the items selected).

i think i will have to back to the old way doing this, user clicks all the check box wanted then press a button, then the items appear in a div.

it would have been really nice to do this with ajax. i am missing something fundamental here.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 19, 2008, 1:05am UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/8 "2008-04-19T01:05:15Z")

</div>

I think you need to read up on some of your object oriented programming. Instance variables are specific to one instance (that's why they're called what they are). If you've got an instance then all the the methods of that object can see its instance variables (which is what underpins your " i have used instance variables across def").

However 2 different instances of an object have completely separate instance variables. The instance of controller that handles your show action and the one that handles your ajax request are different and thus cannot see each other's instance variables. If you want to preserve state across requests then you need to stick it somewhere like the database, the session, or just manage it all client side

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 19, 2008, 1:19am UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/9 "2008-04-19T01:19:04Z")

</div>

All that said, an easier way here might be a Form observer that will send all of your checkboxes to your app whenever any of them are toggled

Fred

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [April 19, 2008, 11:14am UTC](https://discuss.rubyonrails.org/t/instance-variable-ajax-and-partial/28219/10 "2008-04-19T11:14:51Z")

</div>

thanks for your help. you cleared up a lot for me here. hopefully other newbies will gain from it as well.
