ActiveAdmin访问已过滤的集合

时间:2014-10-06 12:11:12

标签: ruby-on-rails ruby activeadmin

我正在尝试创建一个collection_action,我将对整个过滤项目集合做一些事情。我的问题是,在collection_action中,我似乎无法访问过滤后的集合。当我访问collection时,它只是第一页记录中的项目。在我的action_item中,我可以访问collection_before_scope,这正是我想要的过滤记录,但当我尝试从collection_action中访问它时,这是空的。

以下是我的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

2 个答案:

答案 0 :(得分:4)

我知道这已经过时了,但我在尝试访问已过滤的集合以进行自定义CSV下载时遇到了此问题。

由于ActiveAdmin使用Ransack进行搜索,因此您可以使用其参数来获取已过滤的集合。

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

答案 1 :(得分: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