tôi đã cùng một vấn đề, nhưng tôi cần tùy chỉnh chọn lọc trong bộ lọc chỉ số và đầu vào hình thức, vì vậy tôi đã tìm thấy một giải pháp tương tự: Trong ứng dụng/đầu vào (như đề nghị formtastic) tôi tạo ra hai clases:
Trong ứng dụng/đầu vào/country_select_input.rb:
class CountrySelectInput < Formtastic::Inputs::SelectInput
def collection
I18nCountrySelect::Countries::COUNTRY_CODES.map { |country_code|
translation = I18n.t(country_code, scope: :countries, default: 'missing')
translation == 'missing' ? nil : [translation, country_code]
}.compact.sort
end
end
Trong ứng dụng/đầu vào/filter_country_select_input.r b:
class FilterCountrySelectInput < ActiveAdmin::Inputs::FilterSelectInput
def collection
I18nCountrySelect::Countries::COUNTRY_CODES.map { |country_code|
translation = I18n.t(country_code, scope: :countries, default: 'missing')
translation == 'missing' ? nil : [translation, country_code]
}.compact.sort
end
end
Và trong ứng dụng của tôi/admin/city.rb:
ActiveAdmin.register City do
index do
column :name
column :country_code, sortable: :country_code do |city|
I18n.t(city.country_code, scope: :countries)
end
column :created_at
column :updated_at
default_actions
end
filter :name
filter :country_code, as: :country_select
filter :created_at
form do |f|
f.inputs do
f.input :name
f.input :country_code, as: :country_select
end
f.actions
end
end
Như bạn thấy, ActiveAdmin tìm kiếm Lọc [: your_custom_name:] Input và [: your_custom_name:] Input trong ngữ cảnh khác nhau, bộ lọc chỉ mục hoặc đầu vào biểu mẫu. Vì vậy, bạn có thể tạo ra các clases này mở rộng của ActiveAdmin :: Inputs :: FilterSelectInput hoặc Formtastic :: Inputs :: SelectInput và tùy chỉnh logic của bạn.
Nó phù hợp với tôi, tôi hy vọng bạn có thể tìm thấy nó hữu ích