如何使用act-as-votable设置多选项投票系统?

我想使用act-as-votable实现投票系统,我可以提供多种自定义选项 – 例如5个按钮(’蓝色’,’红色’,’绿色’,’灰色’,’白色’)。

我希望我的用户只能选择其中一种颜色,但我希望能够计算每个项目的所有投票(10 – 蓝色,4 – 红色等)。

我觉得我会使用投票范围,但我不太确定如何。

我如何通过行为投票来做到这一点?

看起来很简单:

https://github.com/ryanto/acts_as_votable#examples-with-scopes

@item.vote_by voter: @user1, vote_scope: 'blue' @item.vote_by voter: @user2, vote_scope: 'red' @item.votes_for.size # => 2 @item.find_votes_for(vote_scope: 'blue').size # => 1 @item.find_votes_for(vote_scope: 'red').size # => 1 

因此,您需要在页面上设置一组5个单选按钮(5种颜色)供用户选择,并将选定的参数发送到控制器,您可以使用所选颜色创建投票。

然后,您可以检查用户是否为此项目投票并禁用其未来投票:

 @user.voted_for? @item # => true 

根据评论更新

 params: {id: 1, scope: 'green'} @item = Item.find(params[:id]) scope = params[:scope] if ['red', 'blue', 'green'].include? scope @item.vote_by voter: current_user, vote_scope: scope else # show error message end