我的應用程序是gem acts_as_votable。用戶創建的每篇文章都有upvotes和downvotes。Ruby On Rails用戶排名
我在測試和Postgresql上使用Sqlite3開發,生產。
posts_controller.rb
class PostsController < ApplicationController
def upvote
@post = Post.find(params[:id])
@post.liked_by current_user
redirect_to posts_path
end
def downvote
@post = Post.find(params[:id])
@post.downvote_from current_user
redirect_to posts_path
end
end
users_controller.rb
class UsersController < ApplicationController
def ranking
@users = User.all
end
end
的routes.rb
Rails.application.routes.draw do
resources :posts do
member do
put "like" => "posts#upvote"
put "unlike" => "posts#downvote"
end
end
ranking.html.erb
<% @users.each_with_index do |user,index| %>
<div class="panel panel-default col-md-offset-4 col-md-4 right">
<div class="center">
<h4><b>#<%= index+1 %><b></h4>
</div>
<ul class="nav navbar-nav navbar-left margin-left">
<%if user.banned? %>
<h5 class="red left">[BANNED]</h5>
<%end%>
<h4> <%=link_to user.name, user%></h4>
</ul>
<ul class="nav navbar-nav navbar-right margin-right">
<h4>Points:<%= user.posts.sum(&:cached_votes_score)%></h4>
</ul>
有沒有一種簡單的方法來創建一個按帖子排序的用戶排名?
你用什麼數據庫? –
sqlite3在生產,開發測試和postgre上 – Michal