如何允许单个用户使用'acts_as_votable'gem在单个帖子上多次投票

问题描述 投票:0回答:1

我已经安装了'acts_as_votable'宝石,并且能够成功投票。当我从文档中添加代码以便允许单个用户在单个帖子上多次投票时,它似乎不起作用。

def upvote
    @video = Video.find(params[:id])
    @user = User.first
    @video.upvote_by @user, :duplicate => true
    redirect_to :back
end

def downvote
    @video = Video.find(params[:id])
    @video.downvote_by User.first, :duplicate => true
    redirect_to :back
end
ruby-on-rails
1个回答
1
投票

Acts As Votable文档声明您可以将duplicate: true发送到vote_by,如:

@video.vote_by voter: @user, duplicate: true

现在,看一下源代码,看起来upvote_byvote_updocs的别名,看着vote_up definition

def vote_up(voter, options = {})
  self.vote_by voter: voter, vote: true, vote_scope: options[:vote_scope], vote_weight: options[:vote_weight]
end

它永远不会将duplication选项传递给vote_by

所以,你的解决方案是使用vote_by代替。

© www.soinside.com 2019 - 2024. All rights reserved.