简体   繁体   English

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

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

I am using activeadmin for creating admin module of an application(Ruby on Rail 4.2 App). 我使用activeadmin来创建应用程序的管理模块(Ruby on Rail 4.2 App)。 I have a model car which will be validated from admin side. 我有一辆模型车,将从管理员方面进行验证。 One thing that i don't understand is that how can i filter data from model. 我不明白的一件事是我如何从模型中过滤数据。 You may consider following as what i am trying to do. 您可以考虑遵循我想要做的事情。

Assume there are 10 records in your model/table for cars say ' car ' with a special field/attribute ' car_type ' which can take values only from 假设您的模型/表格中有10条记录显示“ car ”,其中包含一个特殊字段/属性“ car_type ”,它只能从

01 - hatchback 01 - 掀背车

02 - sports_car 02 - sports_car

03 - sedan 03 - 轿车

04 - van 04 - 面包车

How can i show only records with car_type as van 如何只显示car_typevan的记录

My car.rb file for model user at dashboard : 我在仪表板上为模型用户提供的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

end 结束

You can use Index Scopes of active admin 您可以使用活动管理员的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

Index Scopes will show filter at the top of index with following options 索引范围将在索引顶部显示过滤器,并带有以下选项

  • all 所有
  • Hathchback Hathchback
  • Sports Car 跑车
  • Sedan 四门轿车
  • Van 面包车

all will be selected by default - if you want to set Van to be by default 默认选择all - 如果要将Van设置为默认值

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

Customizing the Active Admin Index Page 自定义活动管理员索引页面

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

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