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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/ef0bedb8/attachment-0001.html
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 On 1/17/06, James Ricci <james@riccinursery.com> wrote:> > > > 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
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 -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Will Briggs Sent: Tuesday, January 17, 2006 9:59 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] NOOB Ajax question: handling form input 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 On 1/17/06, James Ricci <james@riccinursery.com> wrote:> > > > Hi, > > > > I was trying to modify how a standard edit form works so I could use Ajaxto> truncate a zip code field which became too long as the user is enteringthe> 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 gettingthere> with the contents of the input field. There''s some trailing garbage, butI''m> thinking maybe the :with option will allow me to pass the value in acleaner> 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
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 Ricci 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 > >-----Original Message----- >From: rails-bounces@lists.rubyonrails.org >[mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Will Briggs >Sent: Tuesday, January 17, 2006 9:59 PM >To: rails@lists.rubyonrails.org >Subject: Re: [Rails] NOOB Ajax question: handling form input > >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 > >On 1/17/06, James Ricci <james@riccinursery.com> wrote: > > > > > > > > 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 > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/c6c465da/attachment.html
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: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Rob Biedenharn Sent: Tuesday, January 17, 2006 10:19 PM To: rails@lists.rubyonrails.org 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 Ricci 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 -----Original Message----- From: rails-bounces@lists.rubyonrails.org [ <mailto:rails-bounces@lists.rubyonrails.org> mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Will Briggs Sent: Tuesday, January 17, 2006 9:59 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] NOOB Ajax question: handling form input 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 On 1/17/06, James Ricci <james@riccinursery.com> wrote:> > > > Hi, > > > > I was trying to modify how a standard edit form works so I could use Ajaxto> truncate a zip code field which became too long as the user is enteringthe> 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 gettingthere> with the contents of the input field. There''s some trailing garbage, butI''m> thinking maybe the :with option will allow me to pass the value in acleaner> 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/3993c3fe/attachment-0001.html
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 On 1/17/06, James Ricci <james@riccinursery.com> 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 > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Will Briggs > Sent: Tuesday, January 17, 2006 9:59 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] NOOB Ajax question: handling form input > > 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 > > On 1/17/06, James Ricci <james@riccinursery.com> wrote: > > > > > > > > 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 > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >