I''m having some trouble getting AJAX working properly for me in Rails. I''ve tried the example in "Agile Web Development with Rails" on page 391 using form_remote_tag(), and instead of updating a <div> like it should, it renders the form as an entirely new page. I''ve tried in on Windows XP and OS X 10.4, WEBrick and Apache, using IE and Firefox, and to no avail. For those who don''t have the book, the problem is essentially: I have a controller with: def index end def blah @stuff = ''should print'' render :partial => ''form'' end index.rhtml: <h3>Display me!</h3> <div id="update_me"> <%= render(:partial => ''form'') %> </div> _form.rhtml: <% if @stuff %> <p> <%= @stuff %> <% end %> <%= form_remote_tag(:update => "update_me", :url => { :action => :blah } ) %> blah blah blah <%= end_form_tag %> When you submit the form, the <h3>Display me!</h3> part doesn''t show again. The render :partial => ''form'' from the ''blah'' action just renders the form and doesn''t update the "update_me" <div> section. The WEBrick output is as follows: 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET /guesswhat HTTP/1.1" 200 851 - -> /guesswhat 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET /favicon.ico HTTP/1.1" 200 0 - -> /favicon.ico 127.0.0.1 - - [12/Dec/2005:10:43:32 Eastern Standard Time] "POST /guesswhat/guess HTTP/1.1" 200 623 http://localhost:3000/guesswhat -> /guesswhat/guess Notice the difference between the first and third actions: - -> /guesswhat http://localhost:3000/guesswhat -> /guesswhat/guess Does anyone know what the problem is? I hope this isn''t too confusing... it''s hard to explain. If you have the book you can run the example yourself and see what you encounter. Thanks in advance, Charlie
make sure you are including the javascript prototype library <%= javascript_include_tag "prototype" %> hope that helps! steve On Mon, December 12, 2005 5:08 pm, Charles Squires wrote:> I''m having some trouble getting AJAX working properly for me in Rails. > I''ve > tried the example in "Agile Web Development with Rails" on page 391 using > form remote tag(), and instead of updating a <div> like it should, it > renders the form as an entirely new page. I''ve tried in on Windows XP and > OS X 10.4, WEBrick and Apache, using IE and Firefox, and to no avail. > > > For those who don''t have the book, the problem is essentially: > > > I have a controller with: > > > def index end > > def blah @stuff = ''should print'' > render :partial => ''form'' end > > > index.rhtml: > <h3>Display me!</h3> > <div id="update me"> > <%= render(:partial => ''form'') %> > </div> > > > form.rhtml: > <% if @stuff %> > <p> <%= @stuff %> > <% end %> > > > <%= form remote tag(:update => "update me", :url => { :action => :blah } > ) > %> > blah blah blah <%= end form tag %> > > > When you submit the form, the <h3>Display me!</h3> part doesn''t show > again. The render :partial => ''form'' from the ''blah'' action just renders > the form and doesn''t update the "update me" <div> section. The WEBrick > output is as follows: > > > 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET > /guesswhat > HTTP/1.1" 200 851 > > > - -> /guesswhat > > > 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET > /favicon.ico > HTTP/1.1" 200 0 > > > - -> /favicon.ico > > > 127.0.0.1 - - [12/Dec/2005:10:43:32 Eastern Standard Time] "POST > /guesswhat/guess HTTP/1.1" 200 623 > > > http://localhost:3000/guesswhat -> /guesswhat/guess > > > Notice the difference between the first and third actions: > - -> /guesswhat > http://localhost:3000/guesswhat -> /guesswhat/guess > > > Does anyone know what the problem is? I hope this isn''t too confusing... > it''s hard to explain. If you have the book you can run the example > yourself and see what you encounter. > > Thanks in advance, > Charlie > > > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
First step, have you included the required java scripts on the page? <%= javascript_include_tag :defaults %> I read through the part and I dont think they mention the needed includes (or at least its hard to spot when they do).
Sorry, I forgot to put that in my example. In the index.rhtml I was importing the JavaScript prototype library. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of stephen karsch Sent: Monday, December 12, 2005 12:25 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] AJAX in Ruby on Rails make sure you are including the javascript prototype library <%= javascript_include_tag "prototype" %> hope that helps! steve On Mon, December 12, 2005 5:08 pm, Charles Squires wrote:> I''m having some trouble getting AJAX working properly for me in Rails. > I''ve > tried the example in "Agile Web Development with Rails" on page 391 > using form remote tag(), and instead of updating a <div> like it > should, it renders the form as an entirely new page. I''ve tried in on > Windows XP and OS X 10.4, WEBrick and Apache, using IE and Firefox, and to no avail. > > > For those who don''t have the book, the problem is essentially: > > > I have a controller with: > > > def index end > > def blah @stuff = ''should print'' > render :partial => ''form'' end > > > index.rhtml: > <h3>Display me!</h3> > <div id="update me"> > <%= render(:partial => ''form'') %> > </div> > > > form.rhtml: > <% if @stuff %> > <p> <%= @stuff %> > <% end %> > > > <%= form remote tag(:update => "update me", :url => { :action => :blah > } > ) > %> > blah blah blah <%= end form tag %> > > > When you submit the form, the <h3>Display me!</h3> part doesn''t show > again. The render :partial => ''form'' from the ''blah'' action just > renders the form and doesn''t update the "update me" <div> section. The > WEBrick output is as follows: > > > 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET > /guesswhat HTTP/1.1" 200 851 > > > - -> /guesswhat > > > 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET > /favicon.ico HTTP/1.1" 200 0 > > > - -> /favicon.ico > > > 127.0.0.1 - - [12/Dec/2005:10:43:32 Eastern Standard Time] "POST > /guesswhat/guess HTTP/1.1" 200 623 > > > http://localhost:3000/guesswhat -> /guesswhat/guess > > > Notice the difference between the first and third actions: > - -> /guesswhat > http://localhost:3000/guesswhat -> /guesswhat/guess > > > Does anyone know what the problem is? I hope this isn''t too confusing... > it''s hard to explain. If you have the book you can run the example > yourself and see what you encounter. > > Thanks in advance, > Charlie > > > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
My last reply was a little vague. In my actual application the import statement is in place, so that is not the cause of the problem. Thanks for the replies, and keep them coming! Thanks again, Charlie -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Charles Squires Sent: Monday, December 12, 2005 1:04 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails] AJAX in Ruby on Rails Sorry, I forgot to put that in my example. In the index.rhtml I was importing the JavaScript prototype library. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of stephen karsch Sent: Monday, December 12, 2005 12:25 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] AJAX in Ruby on Rails make sure you are including the javascript prototype library <%= javascript_include_tag "prototype" %> hope that helps! steve On Mon, December 12, 2005 5:08 pm, Charles Squires wrote:> I''m having some trouble getting AJAX working properly for me in Rails. > I''ve > tried the example in "Agile Web Development with Rails" on page 391 > using form remote tag(), and instead of updating a <div> like it > should, it renders the form as an entirely new page. I''ve tried in on > Windows XP and OS X 10.4, WEBrick and Apache, using IE and Firefox, and to no avail. > > > For those who don''t have the book, the problem is essentially: > > > I have a controller with: > > > def index end > > def blah @stuff = ''should print'' > render :partial => ''form'' end > > > index.rhtml: > <h3>Display me!</h3> > <div id="update me"> > <%= render(:partial => ''form'') %> > </div> > > > form.rhtml: > <% if @stuff %> > <p> <%= @stuff %> > <% end %> > > > <%= form remote tag(:update => "update me", :url => { :action => :blah > } > ) > %> > blah blah blah <%= end form tag %> > > > When you submit the form, the <h3>Display me!</h3> part doesn''t show > again. The render :partial => ''form'' from the ''blah'' action just > renders the form and doesn''t update the "update me" <div> section. The > WEBrick output is as follows: > > > 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET > /guesswhat HTTP/1.1" 200 851 > > > - -> /guesswhat > > > 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET > /favicon.ico HTTP/1.1" 200 0 > > > - -> /favicon.ico > > > 127.0.0.1 - - [12/Dec/2005:10:43:32 Eastern Standard Time] "POST > /guesswhat/guess HTTP/1.1" 200 623 > > > http://localhost:3000/guesswhat -> /guesswhat/guess > > > Notice the difference between the first and third actions: > - -> /guesswhat > http://localhost:3000/guesswhat -> /guesswhat/guess > > > Does anyone know what the problem is? I hope this isn''t too confusing... > it''s hard to explain. If you have the book you can run the example > yourself and see what you encounter. > > Thanks in advance, > Charlie > > > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
You might make sure your <form> has onSubmit=''...return false''. It sounds like may be your browser''s fault--pulling into a new page rather than inserting into the <div>. This will happen if the "submit" is not prevented by "return=false". Rich Clingman Rich-oBnrtXkpV0tBDgjK7y7TUQ@public.gmane.org www.RoboSUV.com AKA www.GrandChallenge.info ----- Original Message ----- From: "Charles Squires" <Charles.Squires-LVvIjLR0h5w@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Monday, December 12, 2005 10:03 AM Subject: RE: [Rails] AJAX in Ruby on Rails Sorry, I forgot to put that in my example. In the index.rhtml I was importing the JavaScript prototype library. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of stephen karsch Sent: Monday, December 12, 2005 12:25 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] AJAX in Ruby on Rails make sure you are including the javascript prototype library <%= javascript_include_tag "prototype" %> hope that helps! steve On Mon, December 12, 2005 5:08 pm, Charles Squires wrote:> I''m having some trouble getting AJAX working properly for me in Rails. > I''ve > tried the example in "Agile Web Development with Rails" on page 391 > using form remote tag(), and instead of updating a <div> like it > should, it renders the form as an entirely new page. I''ve tried in on > Windows XP and OS X 10.4, WEBrick and Apache, using IE and Firefox, and to > no avail. > > > For those who don''t have the book, the problem is essentially: > > > I have a controller with: > > > def index end > > def blah @stuff = ''should print'' > render :partial => ''form'' end > > > index.rhtml: > <h3>Display me!</h3> > <div id="update me"> > <%= render(:partial => ''form'') %> > </div> > > > form.rhtml: > <% if @stuff %> > <p> <%= @stuff %> > <% end %> > > > <%= form remote tag(:update => "update me", :url => { :action => :blah > } > ) > %> > blah blah blah <%= end form tag %> > > > When you submit the form, the <h3>Display me!</h3> part doesn''t show > again. The render :partial => ''form'' from the ''blah'' action just > renders the form and doesn''t update the "update me" <div> section. The > WEBrick output is as follows: > > > 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET > /guesswhat HTTP/1.1" 200 851 > > > - -> /guesswhat > > > 127.0.0.1 - - [12/Dec/2005:10:43:28 Eastern Standard Time] "GET > /favicon.ico HTTP/1.1" 200 0 > > > - -> /favicon.ico > > > 127.0.0.1 - - [12/Dec/2005:10:43:32 Eastern Standard Time] "POST > /guesswhat/guess HTTP/1.1" 200 623 > > > http://localhost:3000/guesswhat -> /guesswhat/guess > > > Notice the difference between the first and third actions: > - -> /guesswhat > http://localhost:3000/guesswhat -> /guesswhat/guess > > > Does anyone know what the problem is? I hope this isn''t too confusing... > it''s hard to explain. If you have the book you can run the example > yourself and see what you encounter. > > Thanks in advance, > Charlie > > > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
FWIW, I''m having the same problem. I also made sure to include the necessary javascripts (:default, which expands to prototype.js and scriptaculous.js). The generated form tag is the following: <div id="vote-form-34" style="border:2px;background:#ddd"> <form action="/browse/vote" method="post" onsubmit="new Ajax.Updater(''vote-form-34'', ''/browse/vote'', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"> and the "return false" statement is there. Still, the request goes through as if it was a regular HTTP request and not an Ajax request. I am using the latest Locomotive download, which includes Rails 0.14.1 IIRC. Ugo -- Ugo Cei Tech Blog: http://agylen.com/ Open Source Zone: http://oszone.org/ Wine & Food Blog: http://www.divinocibo.it/
Ugo Cei wrote:> I am using the latest Locomotive download, which includes Rails 0.14.1 IIRC.In case you didn''t know, you can open Terminal from Locomotive and update its gems as normal, i.e. sudo gem update rails --include-dependencies regards Justin
Il giorno 14/dic/05, alle ore 05:06, Justin Forder ha scritto:> Ugo Cei wrote: > >> I am using the latest Locomotive download, which includes Rails >> 0.14.1 IIRC. > > In case you didn''t know, you can open Terminal from Locomotive and > update its gems as normal, i.e. > > sudo gem update rails --include-dependenciesThank you. I am running 0.14.3 now, but still I have the same problem. However, this looks very much like a client-side problem. The initial page served by Rails looks fine and the response to the subsequent submit looks fine too, it''s just that the browser (Firefox 1.5, OS X Tiger. Safari, by the way, does nothing at all) sends the POST via HTTP and not via XMLHttpRequest. The Javascript libraries are there and Firefox Javascript console shows absolutely no warnings or errors. So, I don''t think even upgrading Rails to 1.0 could change much in this respect. I''m starting to think I need to install Venkman and start tracing through the Prototype code :(. Ugo -- Ugo Cei Tech Blog: http://agylen.com/ Open Source Zone: http://oszone.org/ Wine & Food Blog: http://www.divinocibo.it/
before you do this, be sure to upgrade to 1.0 as the prototype lib is updated in this release. Ugo Cei wrote:> > I''m starting to think I need to install Venkman and start tracing > through the Prototype code :(. >
OK, I found what the problem was: my form consisted only of a group of radio buttons, with no submit button, so I had added a onchange="form.submit()" attribute to every radio button in order to trigger a submit as soon as the button was clicked. Apparently, calling form.submit() completely bypasses the onsubmit handler set for the form, and the Prototype AJAX code was never called! Now I got it to work by removing the onchange handler from buttons and adding a submit button, but this in not exactly what I want. So, my question is: is there a simple way to invoke the onsubmit form handler from the onchange handler of a form control? Ugo -- Ugo Cei Tech Blog: http://agylen.com/ Open Source Zone: http://oszone.org/ Wine & Food Blog: http://www.divinocibo.it/
Ugo Cei wrote:> > Il giorno 14/dic/05, alle ore 05:06, Justin Forder ha scritto: > >> Ugo Cei wrote: >> >>> I am using the latest Locomotive download, which includes Rails >>> 0.14.1 IIRC. >> >> >> In case you didn''t know, you can open Terminal from Locomotive and >> update its gems as normal, i.e. >> >> sudo gem update rails --include-dependencies > > > Thank you. I am running 0.14.3 now, but still I have the same problem. > However, this looks very much like a client-side problem. The initial > page served by Rails looks fine and the response to the subsequent > submit looks fine too, it''s just that the browser (Firefox 1.5, OS X > Tiger. Safari, by the way, does nothing at all) sends the POST via HTTP > and not via XMLHttpRequest. The Javascript libraries are there and > Firefox Javascript console shows absolutely no warnings or errors. So, > I don''t think even upgrading Rails to 1.0 could change much in this > respect.I didn''t think it would solve your problem - I just mentioned it in case you thought you would have to wait for a new version of Locomotive in order to get a more up to date version of Rails.> I''m starting to think I need to install Venkman and start tracing > through the Prototype code :(.Do you have the Pragmatic Ajax beta book? The latest version has a new chapter on debugging Ajax applications. BUT I have just tried this application, using the downloadable code for the AWDR book, and it works for me, both in Safari and Firefox 1.5. I copied the web2 project into my rails projects directory, and opened it in Locomotive (newly upgraded to Rails 1.0), then used rails . in the project directory to bring scripts, javascripts, config etc. up to date (I updated everything except database.yml, but it turns out that it wouldn''t have mattered if I had updated that too, as the demos don''t use a database). Then I tried the list demo and the guesswhat demo, and both work (although I''m not seeing the yellow fade in the list demo). regards Justin
Il giorno 15/dic/05, alle ore 06:05, Justin Forder ha scritto:> BUT I have just tried this application, using the downloadable code > for the AWDR book, and it works for me, both in Safari and Firefox > 1.5.I found what the problem is, and have sent the explanation to this list, but it looks like my message didn''t make it through, so I''ll try to send it again. Ugo -- Ugo Cei Tech Blog: http://agylen.com/ Open Source Zone: http://oszone.org/ Wine & Food Blog: http://www.divinocibo.it/
OK, I found what the problem was: my form consisted only of a group of radio buttons, with no submit button, so I had added a onchange="form.submit()" attribute to every radio button in order to trigger a submit as soon as the button was clicked. Apparently, calling form.submit() completely bypasses the onsubmit handler set for the form, and the Prototype AJAX code was never called! Now I got it to work by removing the onchange handler from buttons and adding a submit button, but this in not exactly what I want. So, my question is: is there a simple way to invoke the onsubmit form handler from the onchange handler of a form control? Ugo -- Ugo Cei Tech Blog: http://agylen.com/ Open Source Zone: http://oszone.org/ Wine & Food Blog: http://www.divinocibo.it/
Did you solve this problem? I''m experiencing the same problem. Thanks, -Kelly On 12/15/05, Ugo Cei <ugo.cei@gmail.com> wrote:> > OK, I found what the problem was: my form consisted only of a group > of radio buttons, with no submit button, so I had added a > onchange="form.submit()" attribute to every radio button in order to > trigger a submit as soon as the button was clicked. Apparently, > calling form.submit() completely bypasses the onsubmit handler set > for the form, and the Prototype AJAX code was never called! > > Now I got it to work by removing the onchange handler from buttons > and adding a submit button, but this in not exactly what I want. So, > my question is: is there a simple way to invoke the onsubmit form > handler from the onchange handler of a form control? > > Ugo > > > -- > Ugo Cei > Tech Blog: http://agylen.com/ > Open Source Zone: http://oszone.org/ > Wine & Food Blog: http://www.divinocibo.it/ > > _______________________________________________ > 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/20060412/9fec8d09/attachment.html