2015-09-25 47 views
0

我在rails 4.2應用程序中使用活動管理員(AA,1.0.0)。我正在顯示在線用戶列表。我想添加'範圍',以便不同類型用戶的鏈接及其計數顯示在主列表上方。活動管理員和過濾器不工作

ActiveAdmin.register User do 
    menu parent: "Users", label: "Online Users", url: '/ admin/users/online_users'                     

    collection_action :online_users, method: :get do 
    @users = User.select{|i| i.online?} 
    end 

    belongs_to :organization, optional: true 
    scope :all, default: true 
    scope :admins do |users| users.with_role(:admin) end 
    scope :type1 do |users| users.with_role(:type1) end 
    scope :type2 do |users| users.with_role(:type2) end 
end 

列表顯示,但範圍不是。我錯過了什麼?

+0

你的意思是'範圍不是'?你錯過了範圍切換器或你是否得到一個空列表?懇求發佈'with_role'的來源 –

+0

它看起來像你試圖使用'collection_action'作爲重定向;你應該使用'scoped_collection'或者一個過濾器。你的示波器看起來不錯。 – ahmacleod

回答

1

您可以使用scoped_collection來限制您對在線用戶的關注,而不是使用collection_action來獲得所需的子集合。這樣,其他一切都按照它應該的那樣工作。

理想情況下,你的users表將有一個online布爾列,在這種情況下,添加一個簡單的where子句將有所斬獲。如果不是,即online?是無法查詢的計算方法,那麼您需要首先計算在線用戶集合id。這不會很好地擴展,所以要小心。

ActiveAdmin.register User do 
    menu parent: "Users", label: "Online Users", url: '/admin/users/online_users' 

    controller do 
    def scoped_collection 
     # if online is a boolean column (best performance): 
     super.where(online: true) 

     # if online is a computed method (convert to ActiveRecord::Relation): 
     # ids = User.all.select(&:online?).map(&:id) 
     # super.where(id: ids) 
    end 
    end 

    belongs_to :organization, optional: true 

    scope :all, default: true 
    scope :admins, -> (u) { u.with_role(:admin) } 
    scope :type1, -> (u) { u.with_role(:type1) } 
    scope :type2, -> (u) { u.with_role(:type2) } 
end 

你也可以做到這一點使用普通users管理路線上的過濾器(假設你有一個)。