2014-10-06 46 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为我工作。