簡體   English   中英

如何開發一個用戶可以投票給其他用戶的投票系統(即類似聲譽的系統)?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM