gpt4 book ai didi

ruby-on-rails - ActiveAdmin 访问过滤集合

转载 作者:数据小太阳 更新时间:2023-10-29 07:26:57 27 4
gpt4 key购买 nike

我正在尝试创建一个 collection_action,我将在其中对整个过滤项目集合执行某些操作。我的问题是,在 collection_action 中,我似乎无法访问过滤后的集合。当我访问 collection 时,只有记录首页上的项目。在我的 action_item 中,我可以访问 collection_before_scope,这正是我想要的过滤记录,但是当我尝试从我的 collection_action 中访问它时,它是空的。

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

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

最佳答案

试试这个:

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

关于ruby-on-rails - ActiveAdmin 访问过滤集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26215985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com