0

Im adding enhancements to an existing rails 5.1 app. I need to add autocomplete with data retrieved from a Mysql database. The application has Jquery, simple_form, and materialize all included. My autocomplete is not working. It is not even getting to the controller. I have tried different examples found online and none work. I am hoping someone can help me.

HTML in HAML code snippet (only snippet)

= simple_form_for @vehicle, url: url_for(controller: 'trip_permits', action: 'update'), html: { method: :patch } do |v|

       .input-field.col.s12.m4
        =v.label :registration_number
        = v.text_field :registration_number, input_html: { class: 'force-upcase' }
     .cssbuttons.alternate
      -# = link_to 'Cancel', '#', onclick: "$('.new_vehicle').hide();", class: 'btn btn-grey'
      %button{ type: 'submit', class: 'btn btn-green', id: 'add'}
        Add
Id of field is generated to trip_permits_vehicles_form_registration_number

JQUERY SNIPPET


`

$(document).ready(function () {

``
            $("#trip_permit_vehicle_form_registration_number").autocomplete(
              {
                  search: function () {},
                  source: function (request, response)
                  {
                      $.ajax(
                        {
                                                url:'/customers/trip_permits/autocomplete_vehicle_registration_number',
                            dataType: "json",
                            data:
                              {
                                  term: request.term,
                              },
                            success: function (data)
                            {
                                response(data);
                            }
                        });
                  },
                  minLength: 2,
                  select: function (event, ui)
                  {
                      var test = ui.item ? ui.item.id : 0;
                      if (test > 0)
                      {
                          alert(test);
                      }
                  }
              });
        }
});

I know I am missing something.  Im not worried right now about the results because I cant even get the ajax call to work.

Can someone please help????

I have tried all the examples I could find online Ive tried select2, simple_form_autocomplete, materialize-autocomplete

1
  • what is search: function () {}, doing? I'm not seeing anything about that in any jqueryUI autocomplete documentation Commented Nov 23, 2022 at 20:54

1 Answer 1

0

How about this?

$("#trip_permit_vehicle_form_registration_number").autocomplete({
  search: function () {},
  source: function(request, response) {
    $.getJSON( 
      "/customers/trip_permits/autocomplete_vehicle_registration_number", 
      {term: request.term },
      response
    );
  },
  minLength: 2,
  select: function (event, ui){
    var test = ui.item ? ui.item.id : 0;
    if (test > 0) { alert(test); }
  }
})
Sign up to request clarification or add additional context in comments.

1 Comment

No, This code isnt working, i set a breakpoint in devtool and its not even stopping. In the code i sent it stops at the autocomlete function but doesnt even seem to be entering it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.