Ruby on Rails: Distinguishing between an upvote and a downvote
I have two buttons, one for upvoting and one for downvoting:
<%= button_to "+1", { :action => "create", :id => @video.id }, :remote => true %>
<%= button_to "-1", { :action => "create", :id => @video.id }, :remote => true %>
They both go to the create method, but I want one to set a video_votes table column named 'val开发者_如何学运维ue' equal to 1 while the other one sets it equal to -1. Where should I do this?
Also, since I'm using AJAX with a create.js.erb file, do I need to do something like respond_to format do format.js
in the create controller method?
You could pass an aditional paramater signifying what type of vote it is.
<%= button_to "+1", { :action => "create", :id => @video.id, :type => "up" }, :remote => true %>
<%= button_to "-1", { :action => "create", :id => @video.id, :type => "down" }, :remote => true %>
And in your def create:
def create
if params[:type] == "up"
#do this
else
# do that
end
end
Also you will have to render the create.erb.js file.
精彩评论