修复投票机制机制

2023-09-10 19:01:16 作者:梦游

我实现了一个向上/向下投票系统,与阿贾克斯在我的Ruby on Rails应用程序更新。该按钮调用创建方法,表决结果插入到数据库中,而投票总和计算。

I've implemented an up/down voting system that updates with ajax in my Ruby on Rails app. The buttons call the create method, a vote is inserted into the database, and the vote sum is calculated.

不过,截至目前,用户可以upvote或downvote多次,他或她会喜欢的。我要投票给像我们在这里看到的计算器,其中一个用户只能投向上或向下一次,票都可以撤销。怎样构建的逻辑呢?

However, as of now, a user can upvote or downvote as many times as he or she would like. I want the voting to be like what we see here on StackOverflow, where a user can only vote up or down once, and the votes can be undone. How do I construct the logic for this?

推荐答案

我会建议 acts_as_rateable 创业板,这是我一直在使用这种类型的多个网站的要求是什么。做这项工作完美。

I'd recommend the acts_as_rateable gem, it's what I've been using for this sort of requirement for multiple websites. Does the job perfectly.

如果你宁愿自己实现,你的评分模型应该有一个 USER_ID 和多态,从而将其自身附加到任何模型,你想进行评分。然后,你可以简单地code的Ajax控制器拒绝重复投票。在前端,使用Javascript,可以消除现有的upvote / downvote链路功能应该为好UX的实施

If you'd rather implement this yourself, your Rating model should have a user_id and be polymorphic so as to attach itself to whatever models you'd like to rate. Then you can simply code your AJAX controller to reject duplicate votes. At the front end, Javascript that removes the link functionality from the existing upvote/downvote should be implemented for good UX.