【问题标题】:ActiveAdmin : How to Filter data from resource. Note: not the search filterActiveAdmin:如何从资源中过滤数据。注意:不是搜索过滤器
【发布时间】:2016-06-24 16:26:33
【问题描述】:

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

假设您的模型/表格中有 10 条汽车记录,例如“car”,其特殊字段/属性“car_type”只能从

01 - 掀背车

02 - sports_car

03 - 轿车

04 - 面包车

我怎样才能仅将带有 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

结束

【问题讨论】:

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


    【解决方案1】:

    您可以使用活跃管理员的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

    【讨论】:

    • 部分正确。我尝试将此范围设置为我的默认范围。但它抛出一个错误:语法错误,意外',',期待keyword_end
    • 这是自定义范围定义为默认值的方式:scope("Instructors", default: true) { |scope| scope.where(:instructor => [2])}
    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2017-12-31
    • 2020-04-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多