我正在使用acts_as_votable gem根据用户获得的多少票数来订购我的链接。
我在视图中使用了一个.each迭代器来处理排序,它工作正常:
@ links.order(cached_votes_up :: desc).each do | link |
但是现在我试图在我的控制器中包含订购功能,但它无法正常工作。我尝试了几种不同的方式:
class LinksController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
def index
@links = Link.order(cached_votes_up: :desc)
@links = Link.paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.js
format.html
end
end
end
class LinksController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
def index
@links = Link.order(cached_votes_up: :desc).paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.js
format.html
end
end
end
class LinksController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
def index
@links = Link.order(cached_votes_up: :desc)
@links.paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.js
format.html
end
end
end
我的_link部分:
<div class="link row clearfix">
<h2>
<%= image_tag link.avatar.thumb %>
</h2>
<h2>
<%= link_to link.title, link %><br>
</h2>
<p>
<%= link_to link.url, link %><br>
</p>
<!-- acts_as_votable for like_link -->
<div class="btn-group">
<%= link_to like_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= link.get_upvotes.size %>
<% end %>
<%= link_to dislike_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-down">
Downvote
<%= link.get_downvotes.size %>
<% end %>
</div>
</div>
index.html.erb:
<div id="link"><%= render @links %></div>
<%= will_paginate @links %>
有谁看到问题可能是什么或我可能做错了什么?
这是我运行Link.order(cached_votes_up: :desc)
时的输出。即使我对这篇文章进行了投票,它仍然将对象作为包含upvote的对象返回?
irb(main):001:0> @link = Link.order(cached_votes_up: :desc).first
Link Load (2.0ms) SELECT "links".* FROM "links" ORDER BY "links"."cached_votes_up" DESC LIMIT 1
=> #<Link id: 16, title: "John F Kennedy", url: "Change is the law of life. And those who look only...", created_at: "2015-12-26 23:56:26", updated_at: "2015-12-26 23:56:31", user_id: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, avatar: "jfk.jpeg">
这是包含acts_as_votable对象的链接模型:
class Link < ActiveRecord::Base
self.per_page = 5
belongs_to :user
validates :user, presence: true
acts_as_votable
attr_accessor :avatar
mount_uploader :avatar, AvatarUploader
end
这是我的路线档案:
Rails.application.routes.draw do
devise_for :users
resources :users
resources :links do
member do
put "like", to: "links#upvote"
put "dislike", to: "links#downvote"
end
end
root to: "links#index"
这些是我的downvote和upvote行动:
def upvote
@link = Link.find(params[:id])
@link.upvote_by current_user
redirect_to :back
end
def downvote
@link = Link.find(params[:id])
@link.downvote_from current_user
redirect_to :back
end
答案 0 :(得分:1)
请试试这个:
class LinksController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
def index
ordered_links = Link.order(cached_votes_up: :desc)
@links = ordered_links.paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.js
format.html
end
end
end
答案 1 :(得分:0)
由于您在评论中说您实际上在注册downvote时遇到了问题,这可能会对您有所帮助
根据acts_as_votable gem docs
默认情况下,所有投票都是正面的
所以
@post.vote_by :voter => @user3
实际上是一个积极的投票。要投票,你需要做这样的事情......
@post.vote_by :voter => @user4, :vote => 'bad'
或者
@post.downvote_from @user2