0

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.

5
  • Please tell more about the functionality that you want. There are many many ways to accomplish this. Commented Oct 6, 2011 at 16:42
  • just a simple counter really. the goal is to create a dynamic scrolling twitter feed. most importantly, it has to be done on client-side without page refreshes. thanks. Commented Oct 6, 2011 at 16:47
  • What do u mean by with javascript, is that js run on serverside or client side? Commented Oct 6, 2011 at 16:50
  • 1
    possible duplicate of How to update variable dynamically within Rails app Commented Oct 6, 2011 at 17:21
  • This is duplicate found over here: stackoverflow.com/questions/7636679/… Commented Oct 18, 2011 at 18:59

1 Answer 1

1

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

session[:counter] = 0

you have the increment code in the controller

def increment_session
  session[:counter] += 1
end

and you will call the 'increment_session' via, AJAX from your view

assuming 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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. i ended up using json to manipulate the data with javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.