Rails, JSON Object, jQuery, Auto-Complete
- by Michael Waxman
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?