How do I create a Up/Down voting system like stackoverflow?
I'm making a site in Rails, and I want to add a vote up/down system like here in Stackoverflow.
Can anyone suggest how to do it? I do know that I'll enter each vote into the database, but I mean, how do I code the vote buttons? What will I use, can anyone help me. Ajax开发者_如何转开发 isn't required but it would be nice.
I was going to try to use a POST command and do something like this,
<form name="input" action="/grinders" method="POST">
<input type="hidden" name="id" value="<%=h grinder.id %>">
<input type="hidden" name="vote" value="up">
<input type="submit" value="Vote" />
</form>
But, I get an authenticity token error, and I honestly do not know how to work with the form helper.
Have a votes
table like so:
[PK] vote_id, vote_type (up/down), [FK] post_id, [FK] user_id, time [optional]
Also add a score
field to your posts
table
Then you could have the vote button access a link like: /vote/post_id/type/
, eg: /vote/14098/up
. This can be done with or without Ajax.
When the vote action is called, check if the user has previously voted on that post - if yes, deny it. If not, create a row with the relevant values in the votes
table and update the score
field in the posts
table.
精彩评论