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

时间:2016-06-24 16:26:34

标签: ruby-on-rails ruby ruby-on-rails-4 activeadmin

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

假设您的车型/表格中有10条记录表示' 汽车'使用特殊字段/属性' car_type '它只能从

获取值

01 - 掀背车

02 - sports_car

03 - 轿车

04 - van

如何仅将 car_type 的记录显示为 van

我在仪表板上的模型用户的 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

1 个答案:

答案 0 :(得分:0)

您可以使用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
  • 跑车
  • Sedan

默认选择所有 - 如果您想将Van设置为默认值

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

Customizing the Active Admin Index Page