Sunday, 13 October 2019

Rails autocomplete with javascript

I'm using Awesomplete to help with autocompletion of a list of towns in a rails 6 application. Everything is working as it should, but once a town is selected it shows the value in the put field instead of the name of the town. Is it possible to show the name once a town is selected but still search by the value?

Form

<%= text_field_tag(:location, value = nil, html_options = {class: 'form-control', id: 'myLocation', placeholder: 'Start typing a town name'}) %>
<%= submit_tag 'Search' %>

Javascript

var input = document.getElementById("myLocation");
    new Awesomplete(input, {
        list: [['Town 1',100],['Town 2',200]...]
    });

Once search is implemented it takes the '100' or '200' value from the array and passes it into the location params.

Controller

data = HTTParty.get("https://www.urlofapi.com/params[:location]}")
@ddcalc = JSON.parse(data.body)


from Rails autocomplete with javascript

No comments:

Post a Comment