Skip to content

Instantly share code, notes, and snippets.

@tylerhunt tylerhunt/Gemfile
Last active Feb 22, 2020

Embed
What would you like to do?
ActiveAdmin association autocomplete without complicated plugins.
@import 'active_admin/mixins';
@import 'active_admin/base';
@import 'jquery.ui.autocomplete';
#= require active_admin/base
#= require jquery.ui.autocomplete
$ ->
$('.autocomplete').each (index, input) ->
$input = $(input)
$hiddenInput = $($(input).data('hidden-input'))
$input
.autocomplete
minLength: 3
delay: 600
source: (request, response) ->
$.ajax
url: $input.data('url')
dataType: 'json'
data:
term: request.term
success: (data) -> response(data)
select: (event, ui) ->
$input.val(ui.item.label)
$hiddenInput.val(ui.item.id)
false
.data('ui-autocomplete')._renderItem = (ul, item) ->
$('<li></li>')
.data('item.autocomplete', item)
.append('<a>' + item.label + '</a>')
.appendTo(ul)
class AutocompleteSerializer < ActiveModel::Serializer
attribute :id
attribute :label
private
def label
object.name
end
end
gem 'active_model_serializers'
gem 'activeadmin'
gem 'jquery-ui-rails'
ActiveAdmin.register Post do
form do |f|
f.inputs 'Details' do
f.input :user,
as: :string,
input_html: {
class: 'autocomplete',
id: 'post_user_name',
name: '',
value: f.object.user.try(:name),
data: {
url: autocomplete_admin_users_path,
hidden_input: '#post_user_id',
},
}
f.input :user_id, as: :hidden
end
end
end
ActiveAdmin.register User do
collection_action :autocomplete, method: :get do
users = User.where('LOWER(name) ILIKE ?', "#{params[:term]}%")
render json: users, each_serializer: AutocompleteSerializer, root: false
end
end
@wrburgess

This comment has been minimized.

Copy link

wrburgess commented Mar 23, 2016

@tylerhunt, using gem "active_model_serializers", "0.9.4" I had to change the attributes on the serializer to

attributes(
    :id,
    :label
  )
@SeriouslyAwesome

This comment has been minimized.

Copy link

SeriouslyAwesome commented Dec 8, 2016

I needed to do this quite a bit in my AA setup, so I abstracted the view stuff to a custom Formtastic input, so that I can now just do f.input :user, as: :autocomplete

See: https://gist.github.com/SeriouslyAwesome/0cf0008150429d4713c62ca6d4a0deee

@carlsmith-aob

This comment has been minimized.

Copy link

carlsmith-aob commented Feb 22, 2020

How might you do this as a filter instead of an input?

@carlsmith-aob

This comment has been minimized.

Copy link

carlsmith-aob commented Feb 22, 2020

Not sure if this is doable in later versions. I'm getting the following error:
Unable to find input class AutocompleteInput
Stacktrace
formtastic (3.1.5) lib/formtastic/helpers/input_helper.rb:336:in rescue in namespaced_input_class' formtastic (3.1.5) lib/formtastic/helpers/input_helper.rb:332:in namespaced_input_class' formtastic (3.1.5) lib/formtastic/helpers/input_helper.rb:342:in input_class' formtastic (3.1.5) lib/formtastic/helpers/input_helper.rb:240:in input' activeadmin (2.6.0) lib/active_admin/filters/forms.rb:14:in filter' activeadmin (2.6.0) lib/active_admin/filters/forms.rb:64:in block (2 levels) in active_admin_filters_form_for' activeadmin (2.6.0) lib/active_admin/filters/forms.rb:57:in each' activeadmin (2.6.0) lib/active_admin/filters/forms.rb:57:in block in active_admin_filters_form_for' actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:45:in block in capture' actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:209:in with_output_buffer' actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:45:in capture' actionview (6.0.2.1) lib/action_view/helpers/form_helper.rb:452:in form_for' activeadmin (2.6.0) lib/active_admin/filters/forms.rb:56:in active_admin_filters_form_for' arbre (1.2.1) lib/arbre/element.rb:181:in method_missing' activeadmin (2.6.0) lib/active_admin/filters/resource_extension.rb:168:in block in filters_sidebar_section' activeadmin (2.6.0) lib/active_admin/views/components/sidebar_section.rb:25:in instance_exec' activeadmin (2.6.0) lib/active_admin/views/components/sidebar_section.rb:25:in build_sidebar_content' activeadmin (2.6.0) lib/active_admin/views/components/sidebar_section.rb:13:in build' arbre (1.2.1) lib/arbre/element/builder_methods.rb:30:in block in build_tag' arbre (1.2.1) lib/arbre/context.rb:92:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:49:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:26:in build_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:39:in insert_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:14:in sidebar_section' activeadmin (2.6.0) lib/active_admin/views/components/sidebar.rb:9:in map' activeadmin (2.6.0) lib/active_admin/views/components/sidebar.rb:9:in build' arbre (1.2.1) lib/arbre/element/builder_methods.rb:30:in block in build_tag' arbre (1.2.1) lib/arbre/context.rb:92:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:49:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:26:in build_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:39:in insert_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:14:in sidebar' active_admin_sidebar (1.2.0) lib/active_admin_sidebar/activeadmin_views_pages_base.rb:16:in build_sidebar' active_admin_sidebar (1.2.0) lib/active_admin_sidebar/activeadmin_views_pages_base.rb:6:in block in build_page_content' arbre (1.2.1) lib/arbre/element/builder_methods.rb:31:in block in build_tag' arbre (1.2.1) lib/arbre/context.rb:92:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:49:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:26:in build_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:39:in insert_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:14:in div' active_admin_sidebar (1.2.0) lib/active_admin_sidebar/activeadmin_views_pages_base.rb:5:in build_page_content' activeadmin (2.6.0) lib/active_admin/views/pages/base.rb:57:in block (2 levels) in build_page' arbre (1.2.1) lib/arbre/element/builder_methods.rb:31:in block in build_tag' arbre (1.2.1) lib/arbre/context.rb:92:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:49:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:26:in build_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:39:in insert_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:14:in div' activeadmin (2.6.0) lib/active_admin/views/pages/base.rb:53:in block in build_page' arbre (1.2.1) lib/arbre/context.rb:92:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:49:in with_current_arbre_element' activeadmin (2.6.0) lib/active_admin/views/pages/base.rb:52:in build_page' activeadmin (2.6.0) lib/active_admin/views/pages/base.rb:9:in build' arbre (1.2.1) lib/arbre/element/builder_methods.rb:30:in block in build_tag' arbre (1.2.1) lib/arbre/context.rb:92:in with_current_arbre_element' arbre (1.2.1) lib/arbre/element/builder_methods.rb:26:in build_tag' arbre (1.2.1) lib/arbre/element/builder_methods.rb:39:in insert_tag' activeadmin (2.6.0) app/views/active_admin/resource/index.html.arb:2 arbre (1.2.1) lib/arbre/context.rb:45:in instance_eval' arbre (1.2.1) lib/arbre/context.rb:45:in initialize' activeadmin (2.6.0) app/views/active_admin/resource/index.html.arb:1:in new' activeadmin (2.6.0) app/views/active_admin/resource/index.html.arb:1 actionview (6.0.2.1) lib/action_view/base.rb:274:in _run' actionview (6.0.2.1) lib/action_view/template.rb:185:in block in render' activesupport (6.0.2.1) lib/active_support/notifications.rb:182:in instrument' actionview (6.0.2.1) lib/action_view/template.rb:386:in instrument_render_template' actionview (6.0.2.1) lib/action_view/template.rb:183:in render' actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:59:in block (2 levels) in render_template' actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:89:in block in instrument' activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in block in instrument' activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in instrument' activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in instrument' actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:88:in instrument' actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:58:in block in render_template' actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:66:in render_with_layout' actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:57:in render_template' actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:13:in render' actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:61:in render_template_to_object' actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:29:in render_to_object' actionview (6.0.2.1) lib/action_view/rendering.rb:118:in block in _render_template' actionview (6.0.2.1) lib/action_view/base.rb:304:in in_rendering_context' actionview (6.0.2.1) lib/action_view/rendering.rb:117:in _render_template' actionpack (6.0.2.1) lib/action_controller/metal/streaming.rb:219:in _render_template' actionview (6.0.2.1) lib/action_view/rendering.rb:103:in render_to_body' actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:52:in render_to_body' actionpack (6.0.2.1) lib/action_controller/metal/renderers.rb:142:in render_to_body' actionpack (6.0.2.1) lib/abstract_controller/rendering.rb:25:in render' actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:36:in render' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:44:in block (2 levels) in render' activesupport (6.0.2.1) lib/active_support/core_ext/benchmark.rb:14:in block in ms' /home/carlsmith/.rbenv/versions/2.7.0/lib/ruby/2.7.0/benchmark.rb:308:in realtime' activesupport (6.0.2.1) lib/active_support/core_ext/benchmark.rb:14:in ms' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:44:in block in render' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:85:in cleanup_view_runtime' activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:34:in cleanup_view_runtime' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:43:in render' responders (3.0.0) lib/action_controller/responder.rb:240:in default_render' responders (3.0.0) lib/action_controller/responder.rb:172:in to_html' responders (3.0.0) lib/responders/flash_responder.rb:109:in to_html' responders (3.0.0) lib/action_controller/responder.rb:165:in respond' responders (3.0.0) lib/action_controller/responder.rb:158:in call' responders (3.0.0) lib/action_controller/respond_with.rb:213:in respond_with' inherited_resources (1.11.0) lib/inherited_resources/actions.rb:7:in index' activeadmin (2.6.0) lib/active_admin/resource_controller/streaming.rb:12:in index' actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in send_action' actionpack (6.0.2.1) lib/abstract_controller/base.rb:196:in process_action' actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:30:in process_action' actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:42:in block in process_action' activesupport (6.0.2.1) lib/active_support/callbacks.rb:135:in run_callbacks' actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:41:in process_action' actionpack (6.0.2.1) lib/action_controller/metal/rescue.rb:22:in process_action' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:33:in block in process_action' activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in block in instrument' activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in instrument' activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in instrument' actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:32:in process_action' actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:245:in process_action' activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:27:in process_action' actionpack (6.0.2.1) lib/abstract_controller/base.rb:136:in process' actionview (6.0.2.1) lib/action_view/rendering.rb:39:in process' actionpack (6.0.2.1) lib/action_controller/metal.rb:191:in dispatch' actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in dispatch' actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:51:in dispatch' actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:in serve' actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:49:in block in serve' actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in each' actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in serve' actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:837:in call' rack (2.1.2) lib/rack/tempfile_reaper.rb:17:in call' rack (2.1.2) lib/rack/etag.rb:27:in call' rack (2.1.2) lib/rack/conditional_get.rb:27:in call' rack (2.1.2) lib/rack/head.rb:14:in call' actionpack (6.0.2.1) lib/action_dispatch/http/content_security_policy.rb:18:in call' rack (2.1.2) lib/rack/session/abstract/id.rb:269:in context' rack (2.1.2) lib/rack/session/abstract/id.rb:263:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/cookies.rb:648:in call' activerecord (6.0.2.1) lib/active_record/migration.rb:567:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in block in call' activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:in run_callbacks' actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in call' rollbar (2.23.2) lib/rollbar/middleware/rails/rollbar.rb:25:in block in call' rollbar (2.23.2) lib/rollbar.rb:145:in scoped' rollbar (2.23.2) lib/rollbar/middleware/rails/rollbar.rb:22:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in call' rollbar (2.23.2) lib/rollbar/middleware/rails/show_exceptions.rb:22:in call_with_rollbar' web-console (4.0.1) lib/web_console/middleware.rb:132:in call_app' web-console (4.0.1) lib/web_console/middleware.rb:28:in block in call' web-console (4.0.1) lib/web_console/middleware.rb:17:in catch' web-console (4.0.1) lib/web_console/middleware.rb:17:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in call' railties (6.0.2.1) lib/rails/rack/logger.rb:38:in call_app' railties (6.0.2.1) lib/rails/rack/logger.rb:26:in block in call' activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in block in tagged' activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:in tagged' activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in tagged' railties (6.0.2.1) lib/rails/rack/logger.rb:26:in call' sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:in call' rack (2.1.2) lib/rack/method_override.rb:24:in call' rack (2.1.2) lib/rack/runtime.rb:24:in call' activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:in call' rack (2.1.2) lib/rack/sendfile.rb:113:in call' actionpack (6.0.2.1) lib/action_dispatch/middleware/host_authorization.rb:83:in call' rack-cors (1.1.1) lib/rack/cors.rb:100:in call' railties (6.0.2.1) lib/rails/engine.rb:526:in call' puma (4.3.1) lib/puma/configuration.rb:228:in call' puma (4.3.1) lib/puma/server.rb:681:in handle_request' puma (4.3.1) lib/puma/server.rb:472:in process_client' puma (4.3.1) lib/puma/server.rb:328:in block in run' puma (4.3.1) lib/puma/thread_pool.rb:134:in block in spawn_thread'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.