0

I am a new Ruby on Rails developer working on an app that uses the easy-Autocomplete plug-in. I have followed a tutorial on it and cannot get it to fully work. The javascript is not executing automatically and when manually invoked, the resulting JSON is not being handled.

The javascript that uses the entered data and causes a call to the server is working. And the controller does the search and creates the JSON object.

But the JSON never seems to make it back to the browser as the field never displays the dropdown with the returned data. (On the server side I have verified that the right data is being generated). 

Another problem is that the javascript in the Assets file is never executed. I only get the javascript to execute on the field if I manually enter it into the Chrome Devtools console. Once that is entered the server call fires off. But again the resulting JSON is not showing up in a dropdown with the field. 

The easy-Autocomplete plug-in and stylesheets are working fine when using a simple javascript in the browser(five colors and typing any letters in the field results in the proper display and selection of from the data array).

#  The Javascript:

document.addEventListener("turbolinks:load", function() {
  $input = $("[id='basics']")

  var options = {
    getValue: "name",
    url: function(phrase) {
      return "/search.json?q=" + phrase;
    },
    categories: [
      {
        listlocation: "users",
        header: "Users",
      }
    ]
  };

  $input.easyAutocomplete(options);
});

#  The controller action:

  def search
    @users = User.ransack(email_cont: params[:q]).result(distinct: true).limit(5)

    respond_to do |format|
      format.html {}
      format.json {
        @users = @users.limit(3)
      }
    end
  end
pstutler
  • 1
  • 2

1 Answers1

0

I got it to work in a new clean app. Required some json builder modifications but the javascript executed and eventually rendered the result. The tutorial I was following had incorrect json builder code for the javascript

pstutler
  • 1
  • 2