我使用acts_as_votable gem來根據他們從用戶獲得的多少票數來訂購我的鏈接。acts_as_votable根據控制器中的upvotes進行排序
我用。每次迭代器在我看來原本處理順序和它工作得很好:
@ links.order(cached_votes_up:DESC)。每個辦|友情鏈接|
但現在我試圖在我的控制器中包含排序功能,它不工作。我嘗試了幾種不同的方式:
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部分:
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,並給予好評的行動:
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
檢查Link.order發出的查詢(cached_votes_up:DESC),看它是否通過created_at使用順序, cached_votes_up –
@DileepNandanam進入控制檯嘗試一下,事實證明,當我在鏈接上投票時,它沒有註冊。相反,它會繼續使用我的'cached_votes_up:1'和'cached_votes_down:0',即使我已經降級並重新啓動控制檯會話。 – Jbur43
@DileepNandanam我貼我所看到的問題 – Jbur43