Ajax

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?

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)?

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

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.

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

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

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