views:

51

answers:

2

I am using the auto complete plugin by Devbridge and I have it all installed here is my code:

$(document).ready(function(){
    $('#request_task').autocomplete({
      serviceUrl: '<%= ajax_path %>',
      minChars:1,
      width: 300,
      delimiter: /(,|;)\s*/,
      deferRequestBy: 0, //miliseconds
      params: { artists: 'Yes' },
    });

});

This request hits my rails action and returns this json. there is only one object returned but most of the time there will be more then 1...this was just a test case:

[
    {
        "user": {
            "salt": "somthing",
            "name": "john",
            "encrypted_password": "92dadsfa6b001ffe71c3c1d8e9fb76c42d1c8afeffa739de9063d94206c",
            "created_at": "2010-09-10T14:10:54Z",
            "updated_at": "2010-09-10T14:10:54Z",
            "admin": null,
            "id": 1,
            "remember_token": "c945522b3eb0a25e36bb39155fc05b3eec301ac5e2196956f2e6f86b4b22c987",
            "email": "[email protected]"
        }
    }
]

I can clearly see the request in firebug but I am not getting anything for the autocomplete and it errors out...Am i missing anything...My error is

a.suggestions is undefined
A: 

YOUR JSON is in a wrong format

Check their correct format

{
 query:'Li',
 suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'],
 data:['LR','LY','LI','LT']
}
From.ME.to.YOU
+3  A: 

I think you need to read a little further down the developers page as your response is in the wrong format:

Web page that provides data for Ajax Autocomplete, in our case autocomplete.ashx will receive GET request with querystring ?query=Li, and it must return JSON data in the following format:

{
 query:'Li',
 suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'],
 data:['LR','LY','LI','LT']
}

Notes:

query - original query value
suggestions - comma separated array of suggested values data
(optional) - data array, that contains values for callback function when data is selected.

Lazarus
This may be the problem as I am not sure about this plugin. But if it the case, it may not be the right solution as you are having tight coupling with format. Try http://docs.jquery.com/Plugins/Autocomplete/autocomplete which can support any format and has very good plugging in capability using very good callback method support
Teja Kantamneni
I wouldn't see this as tightly coupled in the strictest sense. I would expect that my ViewModel (or server side code) would query my data source and format the output accordingly for the View which, in this case, is the Autocomplete. From what I've seen of your alternative you are still going to have to return either an array of strings or an array of key=>value pairs. The documentation isn't good enough to know if your output format above will be read. Lastly, if you don't need all the data in that format, why send it to the client? You could reveal something you shouldn't.
Lazarus