2014-10-06 88 views
3

我正在嘗試創建一個collection_action,我將對整個已過濾項目的集合進行一些操作。我的問題是,從collection_action內我似乎沒有訪問過濾收集。當我訪問collection時,它只是記錄第一頁上的項目。在我的action_item中,我可以訪問collection_before_scope,這正是我想要的過濾記錄,但是當我嘗試從我的collection_action中訪問它時,這是空的。ActiveAdmin訪問過濾的集合

下面是我current setup試圖找到正確的集合。

collection_action :dosmth, :method => :get do 
    # collection_before_scope will be empty 
    puts "collection_before_scope = " + collection_before_scope.to_yaml 

    # collection will return only the filtered items on the front page 
    puts "collection = " + collection.to_yaml 

    redirect_to :back, notice: 'Something happening' 
end 

action_item :only => :index do 
    # collection_before_scope will return the full collection that I'm looking for. 
    puts "collection_before_scope = " + collection_before_scope.to_yaml 

    link_to "Export", dosmth_admin_earned_points_path(controller.params) 
end 

與最接近相關的問題,我能找到的就是這一點,ActiveAdmin Collection action on filtered data,這似乎並沒有幫助我。

任何幫助將不勝感激。

謝謝

更新:

我仍然有同樣的問題,但我已經想通一些東西。如果我嘗試訪問collection_before_scope之前的集合,則正確的過濾項目位於collection_before_scope中。我不想爲了得到正確的collection_before_scope而訪問集合。不知道爲什麼會發生這種情況。

collection_action :dosmth, :method => :get d0 
    # collection will return only the filtered items on the front page 
    puts "collection = " + collection.to_yaml 

    # collection_before_scope will be correct because I accessed the collection first. why??? 
    puts "collection_before_scope = " + collection_before_scope.to_yaml 

    redirect_to :back, notice: 'Something happening' 
end 

回答

3

試試這個:

puts "filtered collection = " + apply_filtering(collection).to_yaml(你叫collection前)

爲什麼你到達正確過濾收集您第一次訪問收集後?

collection方法調用find_collection方法:https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L32

find_collection方法調用apply_filter方法:https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L50

而且一旦collection方法被調用: https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L22-L27

2

我知道這是舊的,但我試圖訪問過濾的集合以進行自定義CSV下載時遇到了此問題。

因爲ActiveAdmin使用搜查搜索,您可以使用其PARAMS搶過濾收集。

ModelName.ransack(params[:q]).result爲我工作。