ActiveAdmin:如何从资源中过滤数据。注意:不是搜索过滤器

ActiveAdmin : How to Filter data from resource. Note: not the search filter

我正在使用 activeadmin 创建应用程序的管理模块(Ruby 在 Rail 4.2 应用程序上)。我有一辆模型车,将从管理员方面进行验证。我不明白的一件事是我如何从模型中过滤数据。你可以考虑按照我正在尝试做的。

假设您的 model/table 中有 10 条汽车记录 'car' 带有特殊 field/attribute 'car_type' 只能从

中获取值

01-两厢车

02 - sports_car

03 - 轿车

04 - 货车

如何只显示 car_typevan

的记录

我的 car.rb 仪表板中模型用户的文件:

ActiveAdmin.register Car do

filter :model_name
filter :model_number


index do
    column :model_name
    column :model_number
    actions defaults: false do |user|
      (link_to 'Sell', "/some route").html_safe
    end 
end

结束

您可以使用 Index Scopes 的活动管理员

ActiveAdmin.register Car do

  filter :model_name
  filter :model_number

  scope :all, default: true
  scope("Hatchback") { |scope| scope.where(car_type: "hatchback") }
  scope("Sports Car") { |scope| scope.where(car_type: "sports_car") }
  scope("Sedan") { |scope| scope.where(car_type: "sedan") }
  scope("Van") { |scope| scope.where(car_type: "van") }

  index do
    column :model_name
    column :model_number
    actions defaults: false do |user|
      (link_to 'Sell', "/some route").html_safe
  end 
end

索引范围将使用以下选项在索引顶部显示过滤器

  • 全部
  • 两厢车
  • 跑车
  • 轿车
  • 面包车

默认情况下会选择所有 - 如果您想将 Van 设置为默认

 scope("Van") { |scope| scope.where(car_type: "van") }, default: true

Customizing the Active Admin Index Page