I have an instance variable in my controller and would like to increment it’s value with Javascript. Am I able to do this? If so, how and where do I put this Javascript file?
A simple counter is all that I’m looking for. I’m not sure if I should look into calling AJAX instead. I need this to be done on the client-side without any page refreshes.
Yes, you will have to use Ajax to update your variable in controller. But I’m not sure how flexible an instance variable for this.
But have your variable in the session instead. Simple work flow will be:
you have a session variable
you have the increment code in the controller
and you will call the ‘
increment_session‘ via, AJAX from your viewassuming you are using Rails < 3, the following are some useful links for implementing AJAX call:
http://railsforum.com/viewtopic.php?id=19606
http://www.ibm.com/developerworks/java/library/j-cb12056/
http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote