-
-
Save varyonic/a054c834aa06e8cc8185a1fbb8cc2d54 to your computer and use it in GitHub Desktop.
ActiveAdmin association autocomplete without complicated plugins.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import 'active_admin/mixins'; | |
@import 'active_admin/base'; | |
@import 'jquery.ui.autocomplete'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#= 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AutocompleteSerializer < ActiveModel::Serializer | |
attribute :id | |
attribute :label | |
private | |
def label | |
object.name | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'active_model_serializers' | |
gem 'activeadmin' | |
gem 'jquery-ui-rails' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment