在ActiveAdmin过滤器中使用ActiveRecord范围-仅在选中时应用过滤器

问题描述 投票:0回答:1

我有一个ActiveRecordFoo,并且我有一个ActiveAdmin索引视图,列出了我所有的foos。Foo的作用域称为bland,我的UI要求是我在Filters部分中具有一个复选框,以便可以看到所有平淡的foos。

即,我希望Filters部分看起来像这样:

enter image description here

我已阅读如下问题和博客,说我需要自定义抢劫犯:

Use ActiveRecord scope in ActiveAdmin filter

所以我的代码是这样的:

ActiveAdmin.register Foo
  filter :bland, label: 'Bland Foos', as: :check_boxes, collection: ['exclude tasty']
end

class Foo < ApplicationRecord
  scope :bland, -> { where("tasty < 3") }
  def self.ransackable_scopes(_auth_object = nil)
    [:bland]
  end
end

但是,当我尝试运行此程序时,出现类似以下错误:

#Ransack :: Search:0x00007f8d18e61db8的未定义方法`bland_in'

哎呀

作为实验,我尝试了类似的操作:

ActiveAdmin.register Foo
  filter :bland, label: 'Bland Foos'
end

class Foo < ApplicationRecord
  scope :bland, -> (dummy_param_not_used) { where("tasty < 3") }
  def self.ransackable_scopes(_auth_object = nil)
    [:bland]
  end
end

我的想法是:好的,ActiveAdmin将放置一个字符串输入,我们将填写一个值只是为了触发代码...但是,过滤器甚至没有显示在我的UI上!

ruby-on-rails activeadmin ransack
1个回答
0
投票

看看这是否有用。您可以根据需要更改UI元素。

ActiveAdmin.register Foo
  ......
  scope :bland, -> { where("tasty < 3") }
  ......
end

参考:ActiveAdmin Documentation

© www.soinside.com 2019 - 2024. All rights reserved.