Created
November 22, 2014 09:09
-
-
Save AlexandrBasan/cc3d9bb96eb9d6514f17 to your computer and use it in GitHub Desktop.
Javascript autocomplete in rails forms from database - http://railscasts.com/episodes/102-auto-complete-association-revised?view=asciicast
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
<%= text_field_tag nil, nil, :id => 'destination_name', data: {autocomplete_source: Airport.order(:name).map { |t| { :label => t.name, :value => t.id } } } %> | |
<%= text_field_tag nil, nil, :id => 'destination_id' %> | |
<script> | |
$("#destination_name").autocomplete({ | |
source:$('#destination_name').data('autocomplete-source'), | |
select: function(e, ui) { | |
e.preventDefault() // <--- Prevent the value from being inserted. | |
$(this).val(ui.item.label) | |
$('#destination_id').val(ui.item.value) | |
} | |
}); | |
//alert("this loaded"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment