简体   繁体   中英

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). 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

01 - hatchback

02 - sports_car

03 - sedan

04 - van

How can i show only records with car_type as van

My car.rb file for model user at dashboard :

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

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
  • Sports Car
  • Sedan
  • Van

all will be selected by default - if you want to set Van to be by default

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

Customizing the Active Admin Index Page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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