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, <script type="text/javascript"> var vote = <%= @voting.vote_count %>; var radiobutton = document.getElementById("radio"); function CheckedValues(){ if(radiobutton.checked){ vote = vote+1 }; }; </script> but it didnt increase the value. any help or advice would be helpful -- Posted via http://www.ruby-forum.com/.
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}) %> <div id="vote_count"></div> 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. -- Posted via http://www.ruby-forum.com/.
Tamilselvi Srinivasan
2009-Jul-29 13:48 UTC
Re: How to update rails Variable in Javascript
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 Blue 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}) %> > <div id="vote_count"></div> > > 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.-- Posted via http://www.ruby-forum.com/.