Rails 活跃管理员免费搜索 select 元素

Rails active admin free search on select element

我正在使用 active admin gem

我有问题 table 与 user_id 有关系,当我想添加新问题时 select 元素显示给大量用户,有一种方法可以让此 select 元素以及用户名的搜索输入?

ActiveAdmin.register Question do

  permit_params :difficulty, :title, :user

  form do |f|
    actions

    inputs 'Question Details' do
      input :user, :as => :select, :collection => User.all

      input :difficulty, as: :select, collection: [1,2,3,4]
      input :title

    end

    actions
  end


end

你可以使用 javascript 插件,我强烈推荐选择 通过简单的 Javascript 调用,它将动态地将常规形式 select 转换为可搜索的 select。

您可以在此处查看一些演示: http://harvesthq.github.io/chosen/

您可以通过将 chosen-rails gem 添加到您的 Gemfile

来将其集成到 activeadmin 中
gem 'chosen-rails'

然后将 javascript 添加到您的 app/assets/javascripts/active_admin.js.coffee

#= require chosen-jquery

将 css 添加到您的 app/assets/stylesheets/active_admin.scss

@import "chosen"

您只需将以下内容添加到 app/assets/javascripts/active_admin.js.coffee

即可将其添加到所有 select 输入
$ -> 
  $("#active_admin_content select").chosen()

您还可以将其限制为某些 select 输入,方法是将 class 添加到活动的管理员输入,并且仅针对那些具有特定 class 的 select,例如 chosen-select