Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexandrBasan/cc3d9bb96eb9d6514f17 to your computer and use it in GitHub Desktop.
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
<%= 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