I am completely new to javascript, and I probably bite more than I can chew, but I'm trying to get a jQuery autcomplete widget on my Rails site.
I have a page called "links" where I want to be able to assign a person to a link. Using autocomplete, I should have a text box that omits the list of names of people suggested by autocomplete, and when they are selected, the username remains in the text box. But when the form is submitted, I do not want the person’s name to be submitted with the form, I want the identity number to be submitted with the form.
So, the question is how to have a person’s name after choosing him, to remain in the text box, but after sending the identifier is sent.
In Rails, this is how I deliver 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
The above produces the following JSON text:
[{"person":{"id":1,"given_name":"dale"}},{"person":{"id":2,"given_name":"sally"}},{"person":{"id":3,"given_name":"joe"}}]
Using the autocomplete jQuery plugin, how can I get the "given name" in the proposed matches, leave the "given name" in the text box after selecting it, but when submitting the form, send the value "id".
I think I need to indicate in javascript what to show on the label and what value to send. Since I'm just getting javascript freeze, if you could explain how your answer works, which will be appreciated. Thanks.
jquery-ui ruby-on-rails autocomplete
Oscar
source share