# NOOB Ajax question: handling form input

**URL:** <https://www.ruby-forum.com/t/noob-ajax-question-handling-form-input/62237>\
**Category:** Rails\
**Created:** [January 18, 2006, 5:34am UTC](https://www.ruby-forum.com/t/noob-ajax-question-handling-form-input/62237 "2006-01-18T05:34:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 18, 2006, 5:34am UTC](https://www.ruby-forum.com/t/noob-ajax-question-handling-form-input/62237/1 "2006-01-18T05:34:49Z")

</div>

Hi,

I was trying to modify how a standard edit form works so I could use  
Ajax to  
truncate a zip code field which became too long as the user is entering  
the  
value. The form was generated by the generate scaffold script. The form  
is  
filled with default data taken from the current record. I coded the  
following changes to a field in the \_form.rhtml file:

\<%= text\_field ‘wh\_config’, ‘zip’ %\>

\<%= observe\_field(:wh\_config\_zip,

```
              :frequency => 0.5,

              :update => :wh_config_zip,

              :url => {:action => :reszip}) %>

```

In the controller, I put the following method in a public section,

```
def reszip

    @fixed_zip = request.raw_post[0..4]

    render(:layout => false)

end

```

An then I created an rhtml file in the view section for the controller  
called reszip.html which contained the following:

\<%= @fixed\_zip %\>

I put a breakpoint at the start of the reszip method, and I am getting  
there  
with the contents of the input field. There’s some trailing garbage, but  
I’m  
thinking maybe the :with option will allow me to pass the value in a  
cleaner  
way.

I would have thought this was simple since Ruby has built in support for  
Ajax, but so far no luck. Am I on the wrong track here?

Thanks in advance.

James

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 18, 2006, 5:37am UTC](https://www.ruby-forum.com/t/noob-ajax-question-handling-form-input/62237/2 "2006-01-18T05:37:50Z")

</div>

I think you’re on the right track. Assign an ID to your form and then  
use the :with attribute to serialize and send your form data to the  
controller.

-Will

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 18, 2006, 5:43am UTC](https://www.ruby-forum.com/t/noob-ajax-question-handling-form-input/62237/3 "2006-01-18T05:43:51Z")

</div>

Will,

Thanks for the amazingly fast response. I looked at the generated html  
source code in my browser:

It looks like my input form has an id already, and I see it referenced  
in  
the java script which gets generated. Can you spot a problem here?

James

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 18, 2006, 5:55am UTC](https://www.ruby-forum.com/t/noob-ajax-question-handling-form-input/62237/4 "2006-01-18T05:55:53Z")

</div>

Why not just change  
size=“30”  
to  
size=“5”  
and skip all the AJAX (for this problem, not completely 😉

-Rob

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 18, 2006, 6:23am UTC](https://www.ruby-forum.com/t/noob-ajax-question-handling-form-input/62237/5 "2006-01-18T06:23:00Z")

</div>

Rob,

I’m trying to understand how Ajax works with Ruby. This is just a simple  
example I thought would be easy to do.

James

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Rob B.  
Sent: Tuesday, January 17, 2006 10:19 PM  
To: removed\_email\_address@domain.invalid  
Subject: RE: [Rails] NOOB Ajax question: handling form input

Why not just change

size=“30”

to

size=“5”

and skip all the AJAX (for this problem, not completely 😉

-Rob

At 1/17/2006 11:02 PM, James R. wrote:

Will,

Thanks for the amazingly fast response. I looked at the generated html  
source code in my browser:

It looks like my input form has an id already, and I see it referenced  
in  
the java script which gets generated. Can you spot a problem here?

James

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [January 18, 2006, 6:38am UTC](https://www.ruby-forum.com/t/noob-ajax-question-handling-form-input/62237/6 "2006-01-18T06:38:06Z")

</div>

Can you post the contents of your .rhtml file? Your input field has  
an ID but I can’t see your form info here or the code you’re using to  
generate the field observer…

Thanks,  
Will
