button_to with :remote=>true doesn't update the vote count on my page
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:
<%= button_to 'Vote', :method => :add_point, :remote=>true %>
In my controller I have,
@video=Video.find(params[:id])
@video.increment!(:votes)
But when butt开发者_Python百科on is clicked, votes value remains same.What is the issue in this code?
Update: Now, I'm getting the following error: Couldn't find Video without an ID
app/controllers/videos_controller.rb:8:in
add_point'`
instead of method
you should use action
. Also check what method you should use with this action (in your routes.rb)
<%= button_to 'Vote', :action => :add_point, :remote=>true %>
First of all, make sure that the action is being completed. Go to your project directory and do:
tail -f log/*.log
Then click the button again while watching your console. If you see some UPDATE sql on your 'videos' table, then you're halfway there.
Secondly, did you write some JS in a view to respond to the Ajax request? Rather than truncate something extremely important, checkout this railscast on Unobtrusive Javascript:
Video: http://railscasts.com/episodes/205-unobtrusive-javascript
Text: http://asciicasts.com/episodes/205-unobtrusive-javascript
Thanks everybody for your valuable feedback.I have tested a non Ajax version and it is working.Only thing is, Iam using default Route.Ajaxing it and adding a Route should be easy enough....or atleast i hope it is. Here's the controller:
def increment
number=Digit.find(params[:id])
number.increment!(:value)
#number.update_attribute(:value,"value+1")
redirect_to(:action=>'index')
end
and here's the view:
<%=button_to '+1',:action=>:increment,:id=>number.id%>
Associating this voting logic with videos or any other content should not be much effort. :)
精彩评论