my dog learned polymorphism*
The moose likes Ruby and the fly likes Incrementing Db field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » Ruby
Bookmark "Incrementing Db field" Watch "Incrementing Db field" New topic
Author

Incrementing Db field

santoshkumar savadatti
Ranch Hand

Joined: Apr 27, 2010
Posts: 95
I have a Vote button in my view. When this button is clicked, it has to increment votes for the particular video.


In my View, I have:


In my controller I have,



But when button is clicked, votes value remains same.What is the issue in this code?
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
Are you saying that when you look directly in the database at the record that you just updated, the votes field has not incremented?

Or are you saying that the code in your view doesn't update?

Or some other thing entirely?
santoshkumar savadatti
Ranch Hand

Joined: Apr 27, 2010
Posts: 95
Sorry.I forgot to update this.
It was a trivial issue.now I have fixed it.
santoshkumar savadatti
Ranch Hand

Joined: Apr 27, 2010
Posts: 95
There was a blooper right in the view:


it should say :action=>:add_point instead of :method=>:add_point.
Also, it wasn't all that surprising that my DB field wasn't getting updated: in that code, there had to be :id=>:video.id
without it, the controller wouldn't know which id to modify.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
well done on figuring it out!
santoshkumar savadatti
Ranch Hand

Joined: Apr 27, 2010
Posts: 95
Thanks again katrina.
But, i want the increment to happen via Ajax so that it doesn't interfere with what is currently being rendered in the view.For example, if i have a video playing, and user clicks "vote" button, the video should continue playing, but the voting should happen in the background.
Right now, I have looked on the net for ways to deal with it, but not finding it.
I have used this in view:

But, not sure what the controller code should look like.So far the code I have seen all involves some form of rendering or redirecting which is not helping at all.
Please point me somewhere.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
Your controller should do something like this:



That way you can update the count in the view in the callback from the javascript.

You will still need to send the id along to your add_points action.

If you're using rails 3, you may want to try something like this in your routes file:



Then your button code can look like this:


This way the video id will be passed to your controller.

You can call things whatever you like, this is just an example. The important things to note here are
1. :to => "controller#action" which needs to match your actual controller and action
2. :as => "some_name" which means you can refer to the route as some_name_path and some_name_url. This is called "named routes" if you want to google it.

Also, remember that you can call "rake routes" an the command line to see what your routes look like and how you can call them.

There are many other ways of writing the route you need. If your route file looks like this:



Then you can add some code like this:



Then call rake routes to see how to refer to this route in you button_to method. Probably something like

Check out the Rails Guide on routing here for more juicy routing goodness: http://edgeguides.rubyonrails.org/routing.html

Katrina
santoshkumar savadatti
Ranch Hand

Joined: Apr 27, 2010
Posts: 95
Thank you very much.
That is very helpful.
santoshkumar savadatti
Ranch Hand

Joined: Apr 27, 2010
Posts: 95
Yes!!
That was awesome.Now, I have really got what i wanted.
Thank you very much.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
You're welcome!
 
jQuery in Action, 2nd edition
 
subject: Incrementing Db field
 
Similar Threads
Vote for JavaRanch!
to view multiple pages of a file or document
array problem
Ajax Captcha
Using Cookies to Prevent Multiple Sends