I''m using ROR 1.0 and the included prototype.js to do some AJAX stuff. I''m using the form_remote_tag to submit a login form using AJAX. So, any error/failed login will be displayed using AJAX. But, for successful login, I would want it to reload the wholepage, not just update the <DIV>. How can I achieve this? -- Posted via http://www.ruby-forum.com/.
On 1/4/06, ngising <ngising@gmail.com> wrote:> I''m using ROR 1.0 and the included prototype.js to do some AJAX stuff. > > I''m using the form_remote_tag to submit a login form using AJAX. > So, any error/failed login will be displayed using AJAX. But, for > successful login, I would want it to reload the wholepage, not just > update the <DIV>. > > How can I achieve this?change the page with javascript. In the :success callback of your form_remote_tag, do something like "location.href = ''foo''" -- rick http://techno-weenie.net
Vivek Krishna
2006-Jan-05 12:03 UTC
[Rails] AJAX to submit form, but reload the whole page
> > > change the page with javascript. In the :success callback of your > form_remote_tag, do something like "location.href = ''foo''"But what about the failure case ? Should I set a response of HTTP to something other than 200 in the controller?so that I can handle it via the failure callback of the Ajax class in the javascript? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060105/a4c7bf07/attachment.html
Vivek Krishna
2006-Jan-05 12:53 UTC
[Rails] AJAX to submit form, but reload the whole page
ok I got it.. render :nothing => true, :status => 401 On 1/5/06, Vivek Krishna <krishna.vivek@gmail.com> wrote:> > > > change the page with javascript. In the :success callback of your > > form_remote_tag, do something like " location.href = ''foo''" > > > But what about the failure case ? Should I set a response of HTTP to > something other than 200 in the controller?so that I can handle it via the > failure callback of the Ajax class in the javascript? > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060105/a9718e04/attachment.html
Carlos Y. Okada
2006-Jan-05 12:58 UTC
[Rails] AJAX to submit form, but reload the whole page
Hi Vivek I think the solution you are looking for is what Jamis Buck teached me a few weeks ago: ... When I''ve needed something like this, I will typically manage things manually. Instead of specifying the :update element, I will handle all of that in a :complete handler by hand. The :complete handler checks the value of a custom header (''X-What-To-Do'', for example). If the header is ''error'', then I update the ''ajax_div'' element with the request.responseText data. If the header is ''ok'', then I execute window.location.reload(false). Does that make sense? If not, let me know and I can post a more detailed example. - Jamis ... here''s a snippet from the hypothetical RHTML file: <script type="text/javascript"> function handleComplete(request) { var instruction = request.getResponseHeader(''X-Instruction'') if(instruction == "error") { $(''ajax_div'').innerHTML = request.responseText } else { window.location.reload(false) } } </script> <div id="ajax_div"></div> <%= form_remote_tag :complete=>''handleComplete(request)'', :url=>{:controller=>''matters_crud'', :action =>:update, :id => @matter} %> <%= render_partial ''form'' %> <%= submit_tag ''Edit'' %> </form> Then, in your controller, you just do: def update @matter = Matter.find(params[:id]) if @matter.update_attributes(params[:matter]) headers[''X-Instruction''] = "ok" render :nothing => true else headers[''X-Instruction''] = "error" render :partial => ''error'' end end - Jamis .... Hope that helps Okada. 2006/1/5, Vivek Krishna <krishna.vivek@gmail.com>:> > > > change the page with javascript. In the :success callback of your > > form_remote_tag, do something like " location.href = ''foo''" > > > But what about the failure case ? Should I set a response of HTTP to > something other than 200 in the controller?so that I can handle it via the > failure callback of the Ajax class in the javascript? > > > _______________________________________________ > 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/20060105/f2dff1af/attachment.html
Vivek Krishna
2006-Jan-05 13:16 UTC
[Rails] AJAX to submit form, but reload the whole page
Hi Carlos, Thanks for the example..Thats what I was looking for .Just one small thing. Whats the false parameter you pass to window.location.reload(). I actually want to do something like redirecting to the user''s homepage if the password/username is correct. So I suppose something like this should work. document.href=''/matters_crud/home'' Vivek On 1/5/06, Carlos Y. Okada <carlos.okada@gmail.com> wrote:> > Hi Vivek > > I think the solution you are looking for is what Jamis Buck teached me a > few weeks ago: > ... > > When I''ve needed something like this, I will typically manage things > manually. Instead of specifying the :update element, I will handle > all of that in a :complete handler by hand. The :complete handler > > checks the value of a custom header (''X-What-To-Do'', for example). If > the header is ''error'', then I update the ''ajax_div'' element with the > request.responseText data. If the header is ''ok'', then I execute > window.location.reload > (false). > > Does that make sense? If not, let me know and I can post a more > detailed example. > > - Jamis > ... > > here''s a snippet from the hypothetical RHTML file: > > <script type="text/javascript"> > > function handleComplete(request) { > var instruction = request.getResponseHeader(''X-Instruction'') > if(instruction == "error") { > $(''ajax_div'').innerHTML = request.responseText > > } else { > window.location.reload(false) > } > } > </script> > > <div id="ajax_div"></div> > <%= form_remote_tag :complete=>''handleComplete(request)'', > > :url=>{:controller=>''matters_crud'', > :action =>:update, > :id => @matter} %> > <%= render_partial ''form'' %> > <%= submit_tag ''Edit'' %> > > </form> > > Then, in your controller, you just do: > > def update > @matter = Matter.find(params[:id]) > if @matter.update_attributes(params[:matter]) > headers[''X-Instruction''] = "ok" > > render :nothing => true > else > headers[''X-Instruction''] = "error" > render :partial => ''error'' > end > end > > - Jamis > .... > > Hope that helps > > > Okada. > > > 2006/1/5, Vivek Krishna <krishna.vivek@gmail.com>: > > > > > > > change the page with javascript. In the :success callback of your > > > form_remote_tag, do something like " location.href = ''foo''" > > > > > > But what about the failure case ? Should I set a response of HTTP to > > something other than 200 in the controller?so that I can handle it via the > > failure callback of the Ajax class in the javascript? > > > > > > _______________________________________________ > > 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/20060105/9d0e782e/attachment.html
Boram Yoon
2006-Jan-29 15:00 UTC
[Rails] Re: AJAX to submit form, but reload the whole page
Hi all: I''m doing something similar to this but for an account signup controller. So once the user successfully creates an account, the user is redirected to index.rhtml by window.location.href = "http://localhost:3000"; I have a problem where the session hash gets deleted on the first time through the process, but not deleted on subsequent times. I''d like to preserve session[:user_id] from a successful account creation/login, but it never works the first time. I can see session[:user_id] = @user.id being set in irb, but after the redirect session[:user_id] = nil. And as previously stated, this works when I create a new account during the same browser session. Has anyone else had this problem? Boram -- Posted via http://www.ruby-forum.com/.
Boram Yoon
2006-Jan-29 15:02 UTC
[Rails] Re: AJAX to submit form, but reload the whole page
Clarification:> as previously stated, this works when I create a new account during the > same browser session.should read> as previously stated, this works when I subsequently create new accounts during > the same browser session.-- Posted via http://www.ruby-forum.com/.
Jonathan Viney
2006-Jan-30 00:13 UTC
[Rails] Re: AJAX to submit form, but reload the whole page
I would have thought that RJS provides a much cleaner solution. update.rjs: if @matter.valid? page << ''location.reload(false);'' else page.replace_html ''ajax_div'', :partial => ''error'' end -Jonny. -- Posted via http://www.ruby-forum.com/.