# Ajax

**URL:** <https://www.ruby-forum.com/t/ajax/83319>\
**Category:** Rails\
**Created:** [January 25, 2007, 3:11am UTC](https://www.ruby-forum.com/t/ajax/83319 "2007-01-25T03:11:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f6c823/32.png) [@shawn](https://www.ruby-forum.com/u/shawn)\
**Post date:** [January 25, 2007, 3:11am UTC](https://www.ruby-forum.com/t/ajax/83319/1 "2007-01-25T03:11:38Z")

</div>

Is there a way to update form field data using Ajax? For example, I  
want a select field with, say, phone numbers in a form. Then I’ll use  
observe\_field to monitor changes, and when the user selects a phone  
number, the ajax call will update a description field in the same form  
with, say, the person’s name related to the phone number.

I’ve successfully got the ajax call working, and, since the second field  
is in a table, I’ve given the table cell an id and told the ajax call to  
update that cell. It then rebuilds the input field with the new data.  
It all works fine, and the new data appears in the correct field as  
expected, but when I post the form, it treats the description field as  
empty.

I’ve tried submitting the form by just typing data in the description  
field, and it works correctly, but if I select something in the select  
field and it updates the description field, the form always treats it as  
empty.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![shawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f6c823/32.png) [@shawn](https://www.ruby-forum.com/u/shawn)\
**Post date:** [January 25, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/ajax/83319/2 "2007-01-25T16:04:31Z")

</div>

Thanks for the reply. It sounds like exactly what I’m trying to do, and  
forgive my ignorance, but I still can’t get it to work.

> You can simply change the field value
> 
> page[‘your\_object\_your\_field’].value=@your\_object.your\_field

Where do I put the above statement? Is it part of the observe\_field  
statement (maybe as a parameter for :complete)?

---

<div class="post-metadata">

**Author:** ![shawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f6c823/32.png) [@shawn](https://www.ruby-forum.com/u/shawn)\
**Post date:** [January 25, 2007, 10:45am UTC](https://www.ruby-forum.com/t/ajax/83319/3 "2007-01-25T10:45:48Z")

</div>

Shawn wrote:

> Is there a way to update form field data using Ajax? For example, I  
> want a select field with, say, phone numbers in a form. Then I’ll use  
> observe\_field to monitor changes, and when the user selects a phone  
> number, the ajax call will update a description field in the same form  
> with, say, the person’s name related to the phone number.  
> (…)

You can simply change the field value

page[‘your\_object\_your\_field’].value=@your\_object.your\_field

For select tag, the value to set is the option ID.

If you need to replace select values:

page[‘field\_id’].length=0  
@your\_object.each do |obj|  
page \<\< “$(‘field\_id’).options[$(‘field\_id’).length]=new  
Option(‘#{escape\_javascript(obj.to\_s)}’,#{obj.id})”  
end

Regards  
Massimo  
[http://www.addsw.it](http://www.addsw.it)

---

<div class="post-metadata">

**Author:** ![shawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f6c823/32.png) [@shawn](https://www.ruby-forum.com/u/shawn)\
**Post date:** [January 25, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/ajax/83319/4 "2007-01-25T16:44:50Z")

</div>

Massimo wrote:

> No; you can put the code directly in your controller method
> 
> def phone\_changed  
> desc=(params[:phone])  
> render :update do |page|  
> page[‘your\_object\_your\_field’].value=desc  
> end  
> end
> 
> or as a separate rjs template view (phone\_changed.rjs), assuming you set  
> @desc in controller
> 
> page[‘your\_object\_your\_field’].value=@desc

Ok, got it. I wasn’t aware of the :update form of render.

(Ok, to be honest, Ajax and java and rjs templates are still pretty  
foggy generally.)

Anyway, thanks a lot for the help.

---

<div class="post-metadata">

**Author:** ![shawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f6c823/32.png) [@shawn](https://www.ruby-forum.com/u/shawn)\
**Post date:** [January 25, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/ajax/83319/5 "2007-01-25T17:43:59Z")

</div>

Hi Shawn,

Shawn wrote:

> (Ok, to be honest, Ajax and java and rjs templates  
> are still pretty foggy generally.)

I recommend Cody F.'s RJS tutorial, available in PDF for from  
O’Reilly  
for $10. Best ten bucks I’ve spent on Rails so far. It won’t make you  
an  
expert, but I found it to be a dang good starting point.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![shawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f6c823/32.png) [@shawn](https://www.ruby-forum.com/u/shawn)\
**Post date:** [January 25, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/ajax/83319/6 "2007-01-25T16:25:34Z")

</div>

Shawn wrote:

> Thanks for the reply. It sounds like exactly what I’m trying to do, and  
> forgive my ignorance, but I still can’t get it to work.
> 
> > You can simply change the field value
> > 
> > page[‘your\_object\_your\_field’].value=@your\_object.your\_field
> 
> Where do I put the above statement? Is it part of the observe\_field  
> statement (maybe as a parameter for :complete)?

No; you can put the code directly in your controller method

def phone\_changed  
desc=(params[:phone])  
render :update do |page|  
page[‘your\_object\_your\_field’].value=desc  
end  
end

or as a separate rjs template view (phone\_changed.rjs), assuming you set  
@desc in controller

page[‘your\_object\_your\_field’].value=@desc

---

<div class="post-metadata">

**Author:** ![shawn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f6c823/32.png) [@shawn](https://www.ruby-forum.com/u/shawn)\
**Post date:** [January 25, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/ajax/83319/7 "2007-01-25T19:59:44Z")

</div>

Massimo wrote:

> If you need to replace select values:
> 
> page[‘field\_id’].length=0  
> @your\_object.each do |obj|  
> page \<\< “$(‘field\_id’).options[$(‘field\_id’).length]=new  
> Option(’#{escape\_javascript(obj.to\_s)}’,#{obj.id})”  
> end

Just an fyi for anyone else who might be following this, I had to change

Option(’#{escape\_javascript(obj.to\_s)}’,#{obj.id})"  
to  
Option(’#{escape\_javascript(obj.description.to\_s)}’,#{obj.id})"

in order for it to work.  
Thanks again Massimo.

Bill, thanks for the tutorial recommendation. My brain balks at the  
idea of me trying to stuff some other new knowledge in there, but it’s  
probably inevitable.

Shawn
