# How to update rails Variable in Javascript

**URL:** <https://www.ruby-forum.com/t/how-to-update-rails-variable-in-javascript/172885>\
**Category:** Rails\
**Created:** [July 29, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/how-to-update-rails-variable-in-javascript/172885 "2009-07-29T13:42:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@sts](https://www.ruby-forum.com/u/sts)\
**Post date:** [July 29, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/how-to-update-rails-variable-in-javascript/172885/1 "2009-07-29T13:42:29Z")

</div>

Hi,  
am having the code like this  
\<%=radio\_button(:nominees, count, :onclick =\> ‘CheckedValues();’)%\> \<%=  
response.nomination %\>

where CheckedValues() is a javascript function  
if the radio button is selected, then i want to increase the vote\_count  
value by 1  
vote\_count is the rails variable.  
how can i use this variable in javascript, i have tried as follows,

but it didnt increase the value.  
any help or advice would be helpful

---

<div class="post-metadata">

**Author:** ![sts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@sts](https://www.ruby-forum.com/u/sts)\
**Post date:** [July 29, 2009, 2:27pm UTC](https://www.ruby-forum.com/t/how-to-update-rails-variable-in-javascript/172885/2 "2009-07-29T14:27:04Z")

</div>

You should use observe\_field with what you are trying to accomplish.

\<%= radio\_button :nominees, :count %\>  
\<%= observe\_field(:nominees\_count, :on=\>‘click’, :frequency=\>0.25,  
:update =\> :vote\_count, :with =\> :nominees\_count, :url =\> { :action =\>  
:nominees\_count\_them}) %\>

Then put the method in your controller:

def nominees\_count\_them

# add your counts here

vote\_count = (perform a find on the model for the current vote count)  
vote\_count += 1

# Do something with your vote count like update to a table

end

The div id is only there so you can view if it’s working. Just look  
through observe\_field and checkbox and radio buttons. You’ll find what  
you need to get it working.

This may or may not work for you. I’m just going by what I remember. I  
believe the syntax is correct though.

---

<div class="post-metadata">

**Author:** ![sts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@sts](https://www.ruby-forum.com/u/sts)\
**Post date:** [July 29, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/how-to-update-rails-variable-in-javascript/172885/3 "2009-07-29T15:48:32Z")

</div>

Thanks for the quick reply Alpha,  
Actually what i want is while selecting the radio button the vote\_count  
should be increased.  
i have tried thst with update\_attribute and increment methods. but the  
vote\_count is not increased .  
could you give some advice about how to increase the vote\_count based on  
the selection.

Alpha B. wrote:

> You should use observe\_field with what you are trying to accomplish.
> 
> \<%= radio\_button :nominees, :count %\>  
> \<%= observe\_field(:nominees\_count, :on=\>‘click’, :frequency=\>0.25,  
> :update =\> :vote\_count, :with =\> :nominees\_count, :url =\> { :action =\>  
> :nominees\_count\_them}) %\>
> 
> Then put the method in your controller:
> 
> def nominees\_count\_them
> 
> # add your counts here
> 
> vote\_count = (perform a find on the model for the current vote count)  
> vote\_count += 1
> 
> # Do something with your vote count like update to a table
> 
> end
> 
> The div id is only there so you can view if it’s working. Just look  
> through observe\_field and checkbox and radio buttons. You’ll find what  
> you need to get it working.
> 
> This may or may not work for you. I’m just going by what I remember. I  
> believe the syntax is correct though.
