# Ajax - Updating Field with Selection

**URL:** <https://www.ruby-forum.com/t/ajax-updating-field-with-selection/173318>\
**Category:** Rails\
**Created:** [August 6, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/ajax-updating-field-with-selection/173318 "2009-08-06T21:07:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 6, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/ajax-updating-field-with-selection/173318/1 "2009-08-06T21:07:41Z")

</div>

Hi all,  
I have two fields in a form.  
One text field non asociated to the model for the user type a value.  
the  
view is:  
\<%= text\_field\_tag ‘item\_cod’,’’, :size =\> 3 %\>

Other a field with a select list with codes, names for the user  
select  
from. The view is:

\<%= f.select :item\_id, Item.select\_list, :size =\> 40 %\>

item\_id is the real field of my model.

I want the following behavior.

1. If the user type a code into the text\_field tag -\> Update the list  
so  
the user view the corresponding name of the item they type.
2. If the user select from the list -\> Update the text field with  
the  
code corresponding to the name selected. (this is the easy part)

My form works fine with one Observe field for the :item\_id list but  
I  
can’t do work the point 1.

Thanks in advance,  
FF

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 6, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/ajax-updating-field-with-selection/173318/2 "2009-08-06T21:41:03Z")

</div>

You probably need to put the select into a partial and then refresh  
the partial when you return from the observe field. I would probably  
go with an ajax “on blur” with the text field as this would put a bit  
less load on the server.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 6, 2009, 9:47pm UTC](https://www.ruby-forum.com/t/ajax-updating-field-with-selection/173318/3 "2009-08-06T21:47:30Z")

</div>

I already have an onblur with the text field and another with the select  
field.  
This works fine, but I don`t know how to represent in the select list  
the  
corresponding name of the code typed in the text field.

2009/8/6 Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 6, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/ajax-updating-field-with-selection/173318/4 "2009-08-06T23:05:56Z")

</div>

Put the select into a partial then do this:

This is what I came up with a while back to re populate a select box  
after a DB add.

Mop is just my main Table / form.

When you add the person to the db then do this:

@mop = Mop.find(params[:id])

```
a = Contact.all.collect{|p| [p.id] }
b = @mop.contacts.all.collect{|p| [p.id] }
c = a-b
ar = Array.new
c.each{|arr|
  arr.each{|p|
    ar << p
  }
}
@contacts = Contact.find(:all, :conditions => ["id IN (?)",

```

ar]).collect {|p| [p.name, p.id] }

In the RJS do this:

page[:contact\_id].replace :partial =\> ‘updateContactList’, :locals =\>  
{ :contacts =\> @contacts, :mop =\> @mop, :person =\> @person }

Partial similar to this:

\<%= select(‘contact’, ‘id’, @contacts, {:prompt =\> ‘Select Contact’},  
{ :onchange =\> remote\_function( :url =\> { :controller =\>  
‘contacts’, :action =\> “ajaxshowcontact”, :id =\> @mop},  
:with =\> “‘contactid=’ + this.value” )  
} ) %\>
