Rails, JSON Object, jQuery, Auto-Complete

Posted by Michael Waxman on Stack Overflow See other posts from Stack Overflow or by Michael Waxman
Published on 2010-06-01T22:48:55Z Indexed on 2010/06/01 22:53 UTC
Read the original article Hit count: 389

I'm using this jquery autocomplete plug-in with rails: http://docs.jquery.com/Plugins/Autocomplete

I can't figure out how to format my results, both in my Rails controller and in my javascript file.

I have something like this in my controller...

@query = params[:q].downcase
@json = User.all(:login => /^#{@query}/)
respond_to do |format|
  format.js  { render :json => @json.to_json(:only => "login"), :layout => false }  
end

And then this in my script.js file...

    $("#form").autocomplete('/url', {
    width: 320,
    dataType: 'json',
    highlight: false,
    scroll: true,
    scrollHeight: 300
    })

But I can't figure out how to parse the data, so my autocomplete just gets a raw array of all my results at once.

How do I process the JSON in the script.js file and/or in my controller for it to work?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ruby-on-rails