<strong>Question: How can I use the acts_as_votable gem to get a "user reputation" system working for my user model? I'm unsure of the best approach given the code below.</strong>
Question: How can I use the acts_as_votable gem to get a "user reputation" system working for my user model? I'm unsure of the best approach given the code below.
Essentially, I have a user model. What I want to do is, for certain users (namely admin accounts), they can access a page where all users are viewable and "vote" for the user in terms of their reputation.
My issue is that, from my understanding, the gem requires both a acts_as_votable, and a acts_as_voter within the model.
However, since the voter is the user, and the votable is the user, too, I'm unsure how to proceed with this.
What I've done so far is:
Console rails generate acts_as_votable:migration rake db:migrate
The model I have is:
class User < ActiveRecord::Base end
Based on this, what would be the recommended solution?
I've tried creating another model, called "UserReputation", and generated relevant controllers and views.
My issue with this approach is that when I'm in the index page, for user reputations, it does not show all the users against a like and dislike button.
I understand why this is the case, as UserReputation is created by a user, and then users would vote for a UserReputation (Analogous to a forum post). This isn't what I want to do, but I was following a tutorial for this.
Essentially, if this is the only way to go, is it possible to put a "link" between User and UserReputation, so they are tied together?
This is my attempted code:
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
Index for User Reputations:
<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>
Understanding the pros and cons of Ruby on Rails versus PHP is important when deciding how to create your business-critical applications.