• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Incrementing Db field

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 1367
18
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry.I forgot to update this.
It was a trivial issue.now I have fixed it.
 
santoshkumar savadatti
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well done on figuring it out!
 
santoshkumar savadatti
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
That is very helpful.
 
santoshkumar savadatti
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes!!
That was awesome.Now, I have really got what i wanted.
Thank you very much.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
reply
    Bookmark Topic Watch Topic
  • New Topic