NOOB Ajax question: handling form input

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

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

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

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

-Rob

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: [email protected]
[mailto:[email protected]] On Behalf Of Rob B.
Sent: Tuesday, January 17, 2006 10:19 PM
To: [email protected]
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 :wink:

-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

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