繁体   English   中英

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

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

我使用activeadmin来创建应用程序的管理模块(Ruby on Rail 4.2 App)。 我有一辆模型车,将从管理员方面进行验证。 我不明白的一件事是我如何从模型中过滤数据。 您可以考虑遵循我想要做的事情。

假设您的模型/表格中有10条记录显示“ car ”,其中包含一个特殊字段/属性“ 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

索引范围将在索引顶部显示过滤器,并带有以下选项

  • 所有
  • Hathchback
  • 跑车
  • 四门轿车
  • 面包车

默认选择all - 如果要将Van设置为默认值

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

自定义活动管理员索引页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM