Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在ActiveAdmin筛选器中使用ActiveRecord作用域--仅在选中时应用筛选器_Ruby On Rails_Activeadmin_Ransack - Fatal编程技术网

Ruby on rails 在ActiveAdmin筛选器中使用ActiveRecord作用域--仅在选中时应用筛选器

Ruby on rails 在ActiveAdmin筛选器中使用ActiveRecord作用域--仅在选中时应用筛选器,ruby-on-rails,activeadmin,ransack,Ruby On Rails,Activeadmin,Ransack,我有一个ActiveRecord类Foo,我有一个列出我所有Foo的ActiveAdmin索引视图。 Foo有一个称为bland的范围,我的UI要求是在过滤器部分中有一个复选框,以便我可以看到所有的bland Foo 也就是说,我希望过滤器部分如下所示: ActiveAdmin.register Foo filter :bland, label: 'Bland Foos', as: :check_boxes, collection: ['exclude tasty'] end class

我有一个
ActiveRecord
Foo
,我有一个列出我所有Foo的ActiveAdmin索引视图。
Foo
有一个称为
bland
的范围,我的UI要求是在过滤器部分中有一个复选框,以便我可以看到所有的bland Foo

也就是说,我希望过滤器部分如下所示:

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
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.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
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.register Foo
过滤器:bland,标签:'bland Foos',as::复选框,集合:['exclude tasty']
结束
类Foo{where(“tasty<3”)}
定义自身可搜索范围(_auth_object=nil)
[:平淡]
结束
结束
但是,当我尝试运行此操作时,会出现如下错误:

用于#Ransack::Search:0x00007f8d18e61db8的未定义方法“bland_in”

哎哟

作为一项实验,我尝试了以下方法:

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
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.register Foo
过滤器:淡而无味,标签:“淡而无味”
结束
类Foo(不使用虚拟参数){where(“tasty<3”)}
定义自身可搜索范围(_auth_object=nil)
[:平淡]
结束
结束

我的想法是:好的,ActiveAdmin将提供一个字符串输入,我们将填充一个值来触发代码。。。但是过滤器甚至没有显示在我的UI上

看看这是否有用。如果需要,您可以更改UI元素

ActiveAdmin.register Foo
  ......
  scope :bland, -> { where("tasty < 3") }
  ......
end
ActiveAdmin.register Foo
......
经营范围:平淡,->{where(“tasty<3”)}
......
结束

参考资料:

应该是集合:[['exclude',false],'tasty',true]?嗯。。。可能是
[['exclude tasty'],true]
这意味着我希望将“exclude tasty”作为文本。我将更新该问题以包含屏幕截图。虽然我试过了,但还是犯了同样的错误。