0

I'm using Merit gem to add reputation system to my application for users that are logged in.

This is an example of how I'm using score to handle voting:

def initialize
  score 5, on: 'posts#upvote', to: :user
  score -5, on: 'posts#downvote', to: :user
  score 1, on: 'posts#upvote', to: :itself
  score -1, on: 'posts#downvote', to: :itself
end

The problem is that with this solution, users can vote on the posts as many times as they want. I would like users to only have a single vote per post. Is there any way to prevent users from voting multiple times?

4

1 回答 1

0

您可以传递一个块来score确定它是否应该允许。有关更多信息,请参阅优点:定义规则

此代码更新了您提供的内容,以举例说明如何在您的应用中实现它:

def initialize
  score 5, on: 'posts#upvote', to: :user {|topic| topic.voted?(@user) }
  score -5, on: 'posts#downvote', to: :user {|topic| topic.voted?(@user) }
  score 1, on: 'posts#upvote', to: :itself {|topic| topic.voted?(@user) }
  score -1, on: 'posts#downvote', to: :itself {|topic| topic.voted?(@user) }
end

这假设您拥有(或可以构建)一种方法来确定用户是否已经对某个主题进行了投票。在这种情况下,该方法是voted?关于您允许投票的主题。

如果您想允许用户只投票一次,但要撤销之前的投票(如 SO 允许的那样),您也可以在块中处理它。块中评估的条件的复杂性完全取决于您。

于 2016-05-21T21:37:32.363 回答