![](/img/trans.png)
[英]How can I create a “reputation” system similar to StackOverflow using Rails?
[英]How can I develop a voting system where users can vote other users (i.e a reputation-like system)?
问题:我如何使用 act_as_votable gem 来获得适用于我的用户模型的“用户声誉”系统? 鉴于以下代码,我不确定最佳方法。
本质上,我有一个用户模型。 我想要做的是,对于某些用户(即管理员帐户),他们可以访问所有用户都可以查看的页面,并根据用户的声誉为用户“投票”。
我的问题是,根据我的理解,gem 需要模型中的acts_as_votable 和acts_as_voter。
但是,由于投票者是用户,而投票者也是用户,因此我不确定如何进行此操作。
到目前为止我所做的是:
宝石文件:
gem 'acts_as_votable'
控制台导轨生成acts_as_votable:migration rake db:migrate
我拥有的模型是:
class User < ActiveRecord::Base
end
基于此,推荐的解决方案是什么?
我尝试创建另一个模型,称为“UserReputation”,并生成相关的控制器和视图。
我对这种方法的问题是,当我在索引页面中时,对于用户声誉,它不会针对喜欢和不喜欢按钮显示所有用户。
我理解为什么会这样,因为 UserReputation 是由用户创建的,然后用户会投票给 UserReputation(类似于论坛帖子)。 这不是我想要做的,但我正在关注这个教程。
从本质上讲,如果这是唯一的方法,是否可以在 User 和 UserReputation 之间放置一个“链接”,以便将它们联系在一起?
这是我尝试的代码:
class User < ActiveRecord::Base
acts_as_voter
has_many :user_reputations
end
class UserReputation < ActiveRecord::Base
acts_as_votable
belongs_to :user
end
class UserReputationsController < ApplicationController
def index
@user_reputations = UserReputation.all
@users = User.all
end
end
用户声誉指数:
<table class="table" style="font-size: 18px;">
<thead>
<tr>
<th> Username </th>
<th> Reputation </th>
<th> Upvote </th>
<th> Downvote </th>
</tr>
</thead>
<tbody>
<% @user_reputations.each do |user_reputation| %>
<tr>
<td> <%= user.email %> </td>
<th> <%= "Reputation" %> </th>
<td> <%= link_to like_user_reputation_path(user), method: :put do %>
like
<%= user_reputation.get_upvotes.size %>
<% end %> </td>
<td> <%= link_to dislike_user_reputation_path(user), method: :put do %>
dislike
<%= user_reputation.get_downvotes.size %>
<% end %> </td>
</tr>
<% end %>
</tbody>
</table>
<br>
由于user_reputation只是每个用户的一条记录,我的观点是在User和UserReputation之间设置has_one而不是has_many关系,这里是模型的设置,注意:你不需要生成迁移,因为user_reputations表已经有user_id字段
在你的模型中
class User < ActiveRecord::Base
acts_as_voter
has_one :user_reputation # new changes
end
class UserReputation < ActiveRecord::Base
acts_as_votable
belongs_to :user
end
在你的控制器中
class UserReputationsController < ApplicationController
def index
@users = User.all
redirect_to user_reputations_path
end
def like_user
@user_reputation = UserReputation.find(params[user_reputation_id])
@user_reputation.liked_by current_user
# current_user is available from application controller if you using bcrypt or devise
end
def dislike_user
@user_reputation = UserReputation.find(params[user_reputation_id])
@user_reputation.downvote_from current_user
end
end
在你看来
<table class="table" style="font-size: 18px;">
<thead>
<tr>
<th> Username </th>
<th> Reputation </th>
<th> Upvote </th>
<th> Downvote </th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td> <%= user.email %> </td>
<td> <%= @user.user_reputation.votes_for.size %> </td>
# here the relation set between user and user_reputation
# since it set one user only one user_reputation
# and you can get scope votes_for since it has acts_as_votable
<td>
<%= link_to "Like", like_user_reputation_path(user_reputation_id: @user.user_reputation.id) %>
</td>
<td>
<%= link_to "dislike", dislike_user_reputation_path(user_reputation_id: @user.user_reputation.id) %>
</td>
</tr>
<% end %>
</tbody>
</table>
在你的路由文件中
resources :user_reputations do
collection {
get :like_user
get :dislike_user
}
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.