I am totally new to javascript and am probably biting of more than I can chew, but I am trying to get the jQuery autcomplete widget working on my Rails site.
I have a page called "links" where I want to be able to assign a person to a link. Using the autocomplete I should be able to have a textbox that drops down a list of peoples names suggested by the autocomplete, and when selected, the persons name remains in the textbox. But when the form is submitted I don't want the persons name sent with the form, I want the persons ID sent with the form.
So the question is how to have the persons name once selected, remain in the textbox, but once submitted the persons id is submitted.
In Rails, this is how I am supplying the JSON data from my Person controller:
def index
if params[:term]
@people = Person.find(:all,:conditions => ['given_name LIKE ?', "#{params[:term]}%"], :limit => 5)
else
@people = Person.all
end
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @people.to_json(:only => [:id, :given_name]) }
end
end
The above outputs the following JSON text:
[{"person":{"id":1,"given_name":"dale"}},{"person":{"id":2,"given_name":"sally"}},{"person":{"id":3,"given_name":"joe"}}]
Using the jQuery autocomplete plugin, how do I have the 'given_name' appear in the suggested matches, leave the 'given_name' in the textbox once selected, but send the 'id' value when the form is submitted.
I think I need to specify in the javascript what to label to show and what value to send. As I am just getting the hang of javascript, if you could explain how your answer works that would be appreciated. Thanks.