3

I'm using the rails3-jquery-autocomplete gem. Everything works beautifully.

My only problem now is figuring out how to display a 'no matches' message when there are no matches for the autocomplete text.

I'm new at this - I've had a look at these similar responses (here and here but still can't seem to figure it out.

Thanks!

Community
  • 1
  • 1
Cat Friend
  • 33
  • 3

1 Answers1

1

I did something like this:


jQuery(".auto_search_complete").live("click", function() {
var $this = jQuery(this);
     $this.autocomplete({
        minLength: 3,

        source: function (request, response) {
        jQuery.ajax({
            url: "/autocomplete.json",
            data: {
            term: request.term
            },
            success: function (data) {
            if (data.length == 0) {
// You would enter a return for printing "No Response" here.
// I did:
    //  $this.siblings('span.guest_email').show();
          //      $this.siblings('span.delete_button').show();

            }
            response(data);
            }
        });
        },
// ... Rest of your stuff here, like focus, select... this above bit is your answer....

I used jQuery because my $ was taken up with prototype so i had to differ.

So notice you have to use the long form of the ajax request, not the fancy quick one, because you want to break up the success into that 'if there is no data clause'

pjammer
  • 9,489
  • 5
  • 46
  • 56
  • dang, I wonder what you put in the if (data.length == 0) section? even `console.log("I am here");` doesn't work? – pjammer Sep 10 '12 at 14:25
  • i just had a no-matches div, and i did $("#no-matches").text("No matches."). I'll mess about with it some more and try to figure out what's gone wrong. – Cat Friend Sep 10 '12 at 15:15
  • If i can remember exactly what lead me to my working code, maybe I changed something else too, but honest, i swear it should work for ya. – pjammer Sep 10 '12 at 15:19
  • Hi! Thanks - I did it without using the rails3-jquery-autocomplete gem and this worked then. – Cat Friend Sep 11 '12 at 19:11