2015-12-30 42 views
1

我使用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 
+0

檢查Link.order發出的查詢(cached_votes_up:DESC),看它是否通過created_at使用順序, cached_votes_up –

+0

@DileepNandanam進入控制檯嘗試一下,事實證明,當我在鏈接上投票時,它沒有註冊。相反,它會繼續使用我的'cached_votes_up:1'和'cached_votes_down:0',即使我已經降級並重新啓動控制檯會話。 – Jbur43

+0

@DileepNandanam我貼我所看到的問題 – Jbur43

回答

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 
+0

建議:「@links = Link.order」「」到「」「ordered_links = Link.order」「」變「」。然後改變「」「@links = @ links.paginate」「」到「」「@links = ordered_links.paginate」「」 – MilesStanfield

+0

我喜歡這種變化@MilesStanfield和楊。我實現了它。事實證明,這未必是我遇到的問題,如果你看看我的更新問題,當我從一個downvote更改爲給予好評我downvotes沒有被註冊。 – Jbur43

+0

用戶可以投票的鏈接多次,而不會覆蓋以前的行爲? – Yang

0

既然你在你的評論說,你註冊一個downvote居然有問題,這可能會幫助你

根據acts_as_votable寶石文檔

默認情況下,所有的票都是正

所以

@post.vote_by :voter => @user3 

實際上是一個積極的投票。向下投你需要做這樣的事情...

@post.vote_by :voter => @user4, :vote => 'bad' 

或者

@post.downvote_from @user2 
+0

我發佈了我的控制器和路由文件。根據你在這裏解釋的內容,我認爲我有,但只是使用'downvote_from'。 – Jbur43

相關問題