我已将Select2与我的Active Admin集成在一起。但是它无法正常工作。它仅显示下拉下的搜索选项。
我遵循以下链接:(Select2-Rails与ActiveAdmin不使用(
使用Ruby版本:Ruby-2.6.3 [X86_64]铁轨版本:Rails 5.0.7.2
这是我的app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require_tree .
//= require select2
//= require select2_locale_pt-BR
$( "#dropdown" ).select2({
language: "zh-TW"
});
这是我的app/assets/stylesheets/application.css
*= require bootstrap
*= require_tree .
*= require_self
*= require select2
*= require select2-bootstrap
我在这里使用了select2,
customer_location.input :area, as: :select2, collection: Region.all.map {|u| [u.area.to_s, u.id]}, :input_html => { :class => 'select2-input', :width => 'auto', "data-placeholder" => 'Select Area' }
我使用了activeadmin-select2 gem
,对我有效。
将activeadmin
,jquery-rails
和select2-rails
添加到您的gemfile:
gem 'activeadmin'
gem 'jquery-rails'
gem 'select2-rails'
并将activeadmin-select2
添加到您的gemfile:
gem 'activeadmin-select2', github: 'mfairburn/activeadmin-select2'
使用:
将ActiveAdmin-select2调用添加到Active_admin样式表和JavaScripts。 @import "active_admin/select2/base";
#add this line to your app/assets/stylesheets/active_admin.scss
#= require active_admin/select2
#add this line to your app/assets/javascripts/active_admin.js
用法:
过滤器
标准:选择过滤器将自动转换为Select2过滤器。如果您想要一个多选组件盒,请使用:
ActiveAdmin.register Products do
filter :fruits, as: :select2_multiple, collection: [:apples, :bananas, :oranges]
end
选择列表
使用select2样式列表只需更改以下更改:select至:select2或:select2_multiple
ActiveAdmin.register Products do
form do |f|
f.input :fruit, as: :select2
end
form do |f|
f.inputs "Product" do
f.has_many :fruits, allow_destroy: true, new_record: "Add Fruit" do |e|
e.input :fruit, as: :select2_multiple
end
end
end
end
对我而言,我通过在gemfile中用select2 gem添加搜索选项将其添加到Active Admin
中的下拉列表来解决它。gem 'activeadmin-searchable_select'
在Active Admin中添加
f.input :address_id, label: "Client Address" , as: :searchable_select, :collection => Address.order(name: :desc).to_a(&:name).collect {|address| [address.name, address.id] }