ActiveAdmin Access过滤的集合 [英] ActiveAdmin Access filtered collection

查看:70
本文介绍了ActiveAdmin Access过滤的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个collection_action,在其中我将对整个过滤后的项目集合进行处理。我的问题是,在collection_action中,我似乎无权访问已过滤的集合。当我访问集合时,只有记录第一页上的项目。在我的action_item中,我可以访问 collection_before_scope ,这正是我想要的过滤记录,但是当我尝试从我的 collection_action中访问它时,此记录为空

I'm trying to create a collection_action where I'm going to do something with the entire collection of filtered items. My problem is that from within the collection_action I don't seem to have access to the filtered collection. When I access collection it is only the items that are on the first page of records. In my action_item I have access to collection_before_scope which is exactly the filtered record I want, but this is empty when I try to access it from within my collection_action.

下面是我的当前设置,试图找到正确的集合。

Below is my current setup attempting to find the correct collection.

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收集操作,似乎并没有帮助我。

The closest related question I could find was this, ActiveAdmin Collection action on filtered data, which didn't seem to help me out.

任何帮助将不胜感激。

谢谢

更新:

我仍然有同样的问题,但是我发现了一些问题。如果我尝试在 collection_before_scope 之前访问集合,则正确的过滤项在collection_before_scope中。我不想只为了获取正确的 collection_before_scope 即可访问收藏集。不知道为什么会这样。

I still have the same problem, but I have figured something out. If I try to access the collection before the collection_before_scope then the correct filtered items are in collection_before_scope. I don't want to have to access the collection just to get the correct collection_before_scope though. Not sure why this would happen.

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


推荐答案

尝试一下:

puts filtered collection = + apply_filtering(collection).to_yaml (在您调用 collection 之前)

为什么您在第一次访问集合后会到达正确的过滤集合?

Why do you reach the correct filtered collection after you accessed the collection first?

集合方法将调用 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

然后调用了收集方法:
https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller /data_access.rb#L22-L27

这篇关于ActiveAdmin Access过滤的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆