Skip to content

Instantly share code, notes, and snippets.

@dmitry
Created December 8, 2012 15:45
Show Gist options
  • Save dmitry/4240801 to your computer and use it in GitHub Desktop.
Save dmitry/4240801 to your computer and use it in GitHub Desktop.
Custom filters/search in sidebar for active_admin using a partial
= form_for @search, url: collection_path,
as: :q,
builder: ActiveAdmin::Filters::CustomFormBuilder,
html: {method: :get, class: :filter_form} do |f|
= f.input :code_contains, as: :string
= f.input :code_contains, as: :filter_string
= f.form_buffers.last
= f.buttons do
= f.submit I18n.t('active_admin.filter')
= link_to(I18n.t('active_admin.clear_filters'), "#", :class => "clear_filters_btn")
= hidden_field_tags_for(params, :except => [:q, :page])
# lib/active_admin/filters/custom_form_builder.rb
module ActiveAdmin
module Filters
class CustomFormBuilder < ::ActiveAdmin::Filters::FormBuilder
def input(method, options = {})
options = options.dup # Allow options to be shared without being tainted by Formtastic
options[:as] ||= default_input_type(method, options)
klass = input_class(options[:as])
obj = klass.new(self, template, @object, @object_name, method, options)
class << obj
include ActiveAdmin::Inputs::FilterBase
end
obj.to_html
end
def custom_input_class_name(as)
"#{as.to_s.camelize}Input"
end
def active_admin_input_class_name(as)
"ActiveAdmin::Inputs::#{custom_input_class_name(as)}"
end
end
end
end
config.clear_sidebar_sections!
sidebar :filters do
render partial: 'search'
end
@jsyoun0823
Copy link

T__T Thank you... very very thank you.... 당신은 천사야

@dmitry
Copy link
Author

dmitry commented Aug 1, 2019

@jsyoun0823 nice that you find it helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment