# Instance variable won't update using basic Ajax script

**URL:** <https://www.ruby-forum.com/t/instance-variable-wont-update-using-basic-ajax-script/126527>\
**Category:** Rails\
**Created:** [January 19, 2008, 1:36am UTC](https://www.ruby-forum.com/t/instance-variable-wont-update-using-basic-ajax-script/126527 "2008-01-19T01:36:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 19, 2008, 1:36am UTC](https://www.ruby-forum.com/t/instance-variable-wont-update-using-basic-ajax-script/126527/1 "2008-01-19T01:36:03Z")

</div>

I have been beating my head against the wall on this for days. It seems  
that I can’t get an instance variable to update using Ajax if it  
contains an array.  
If the instance variable contains a singular value, my Ajax update works  
fine.

In this instance, @listcount contains a number here and works fine:

```auto
<%= periodically_call_remote( :update => "counter",:frequency => 2,:url
=> { :action => :update_counter }) %>
<div id="counter"><%= @listcounts %></div>
```

In this next case, @trap contains an array that I have to iterate  
through in my view and will not update using Ajax:

[code]  
\<%= periodically\_call\_remote( :update =\> “status”,:frequency =\> 2,:url  
=\> { :action =\> :update\_data }) %\>

\<% even\_odd = 0 for trap in @trap even\_odd = 1 - even\_odd %\> \<%=trap.datetime %\> \<%=trap.description %\>

Can anyone point me in the right direction to get @trap to update using  
Ajax?

Here is the controller code for @trap:

```auto
def update_data
        @trap = Trap.find(:all, :order => "datetime DESC")
        render :text => @trap
end
```

Any and all suggested will be treated as gold 🙂  
thanks

jackster.mobi

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 19, 2008, 1:43am UTC](https://www.ruby-forum.com/t/instance-variable-wont-update-using-basic-ajax-script/126527/2 "2008-01-19T01:43:07Z")

</div>

damn strait you can’t do that

 \<%= render :partial =\> 'status', :locals =\> {:traps =\> @trap} %\>

:partial =\> status:

\<% even\_odd = 0 traps.each do |trap| even\_odd = 1 - even\_odd %\>

| \<%=trap.datetime %\> | \<%=trap.description %\> |

def update\_data  
@trap = Trap.find(:all, :order =\> “datetime DESC”)  
render :partial =\> ‘status’, :locals =\> {:traps =\> @trap}  
end

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 19, 2008, 2:38am UTC](https://www.ruby-forum.com/t/instance-variable-wont-update-using-basic-ajax-script/126527/3 "2008-01-19T02:38:15Z")

</div>

thanks alot for the help Keynan…I NEVER would have guessed the syntax  
on that. I googled this problem for hours and found no documentation  
worth anything. I put in the code you suggested, but am getting errors.

I am gettting an error in my view:

```
     wrong number of arguments (0 for 3)

```

it doesn’t like the following line:

```
  <%= render :partial => 'status', :locals => {:traps => @trap} %>

```

It might be a mistake on my interpretation of your instructions, here’s  
what I have in my config based on your advice:  
------------begin view ---------------------------  
\<%= periodically\_call\_remote( :update =\> “status”,:frequency =\> 2,:url  
=\> { :action =\> :update\_data }) %\>

:partial =\> status:
\<%= render :partial =\> 'status', :locals =\> {:traps =\> @trap} %\>

| &nbsp; |

\<% even\_odd = 0 traps.each do |trap| even\_odd = 1 - even\_odd %\> \<% end %\>

| \<%=trap.datetime %\> | \<%=trap.desc1 %\> |

----------------end view -------------------------- ----------------begin controller ------------------ def update\_data @trap = Trap.find(:all, :order =\> "datetime DESC") render :partial =\> 'status', :locals =\> {:traps =\> @trap} end ---------------end controller----------------------- I really appreciate the help on this....thanks.

jackster
