Forum: Ruby on Rails NOOB Ajax question: handling form input

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James R. (Guest)
on 2006-01-18 06:34
(Received via mailing list)
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'  %></p>

<%= 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
Will B. (Guest)
on 2006-01-18 06:37
(Received via mailing list)
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
James R. (Guest)
on 2006-01-18 06:43
(Received via mailing list)
Will,

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

<input id="wh_config_zip" name="wh_config[zip]" size="30" type="text"
value="111113" /></p>
<script type="text/javascript">
//<![CDATA[
new Form.Element.Observer('wh_config_zip', 0.5, function(element, value)
{new Ajax.Updater('wh_config_zip', '/wh_config/reszip',
{asynchronous:true,
evalScripts:true, parameters:value})})
//]]>
</script>

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
Rob B. (Guest)
on 2006-01-18 06:55
(Received via mailing list)
Why not just change
size="30"
to
size="5"
and skip all the AJAX (for this problem, not completely ;-)

-Rob
James R. (Guest)
on 2006-01-18 07:23
(Received via mailing list)
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] 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:

<input id="wh_config_zip" name="wh_config[zip]" size="30" type="text"
value="111113" /></p>
<script type="text/javascript">
//<![CDATA[
new Form.Element.Observer('wh_config_zip', 0.5, function(element, value)
{new Ajax.Updater('wh_config_zip', '/wh_config/reszip',
{asynchronous:true,
evalScripts:true, parameters:value})})
//]]>
</script>

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
Will B. (Guest)
on 2006-01-18 07:38
(Received via mailing list)
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
This topic is locked and can not be replied to.