I am doing an Ajax call in my page to a controller method like so: new Ajax.Request(''<%= url_for(:action => "target_list_add" )%>'', { asynchronous:true });" In my controller method "target_list_add", I do something and then I say: redirect_to( :action => ''target_list_upload'', :layout => false ) I know for certain that this line of code is being reached. Why does the redirect not occur? Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Cody Fauser
2006-Mar-20 18:18 UTC
[Rails] Ajax.Request w/standard redirect doesn''t render
Wes, Since the Ajax call in the background, you need to perform the redirect with JavaScript, so the easiest way would be for you to use inline RJS in your controller like follows: def target_list_add render :update do |page| page.redirect_to( :action => ''target_list_upload'') end end You''ll need Edge Rails or the RJS plugin to do that, though. If you don''t want to use RJS, you could do it the long way like this: def target_list_add @headers[''Content-Type''] = ''text/javascript'' render :text => "window.location.href = ''#{url_for(:action => ''target_list_upload'')}''" end On 3/20/06, Wes Gamble <weyus@att.net> wrote:> I am doing an Ajax call in my page to a controller method like so: > > new Ajax.Request(''<%= url_for(:action => "target_list_add" )%>'', { > asynchronous:true });" > > In my controller method "target_list_add", I do something and then I > say: > > redirect_to( :action => ''target_list_upload'', :layout => false ) > > I know for certain that this line of code is being reached. > > Why does the redirect not occur? > > Thanks, > Wes > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cody Fauser http://www.codyfauser.com
Wes Gamble
2006-Mar-20 18:26 UTC
[Rails] Re: Ajax.Request w/standard redirect doesn''t render
Cody, Thanks for your prompt reply. Can you go into more detail on what "Since the Ajax call in the background" means? I do understand Ajax conceptually, but I''m trying to understand the nature of a HTTP response in an Ajax context. Is the root of my issue that there is no HTTP response to give back to the browser and that is why the Ajax.Request + controller redirect doesn''t render anything? Thanks, Wes Cody Fauser wrote:> Wes, > > Since the Ajax call in the background, you need to perform the > redirect with JavaScript, so the easiest way would be for you to use > inline RJS in your controller like follows: > > def target_list_add > render :update do |page| > page.redirect_to( :action => ''target_list_upload'') > end > end > > You''ll need Edge Rails or the RJS plugin to do that, though. > > If you don''t want to use RJS, you could do it the long way like this: > > def target_list_add > @headers[''Content-Type''] = ''text/javascript'' > render :text => "window.location.href = ''#{url_for(:action => > ''target_list_upload'')}''" > end > > On 3/20/06, Wes Gamble <weyus@att.net> wrote: >> I know for certain that this line of code is being reached. >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > Cody Fauser > http://www.codyfauser.com-- Posted via http://www.ruby-forum.com/.
Emin Hasanov
2006-Mar-20 19:34 UTC
[Rails] Re: Ajax.Request w/standard redirect doesn''t render
Wes, the root of the issue is that what you get back from your request needs to be processed by javascript to be actioned upon. You only get XML data back and your browser will not process it unless told to do that. This is what Cody meant by "call in background". Therefore, if you want to do a conditional redirect you need to analyze received data and if it meets required cretirea do a javascript redirect. On 3/20/06, Wes Gamble <weyus@att.net> wrote:> > I do understand Ajax conceptually, but I''m trying to understand the > nature of a HTTP response in an Ajax context. > > Is the root of my issue that there is no HTTP response to give back to > the browser and that is why the Ajax.Request + controller redirect > doesn''t render anything? > > Thanks, > Wes >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060320/3930c4b9/attachment.html
Justin Forder
2006-Mar-21 03:24 UTC
[Rails] Re: Ajax.Request w/standard redirect doesn''t render
Wes Gamble wrote:> Cody, > > Thanks for your prompt reply. > > Can you go into more detail on what "Since the Ajax call in the > background" means? > > I do understand Ajax conceptually, but I''m trying to understand the > nature of a HTTP response in an Ajax context. > > Is the root of my issue that there is no HTTP response to give back to > the browser and that is why the Ajax.Request + controller redirect > doesn''t render anything?The HTTP request was made by the XMLHTTP component (or equivalent), and the XMLHTTP component handles the resulting HTTP response. If the response specifies a redirect, it is the XMLHTTP component that should be trying again at the redirect address - not the browser. regards Justin