Is this a bug in Ajax handling?

When a controller responds to a link_to_remote with a redirect_to, the
link_to_remote gets a success callback, this would seem like a bug to
me, at a minimum it should return a failure?

This is driving me crazy because all the login engines/generators
respond to an authentication error with a redirect_to. The work around
is to change them to all do a

render :layout => false, :status => 500 if request.xhr?

But that seems a little overkill.

here is my simple test… clicking the first link returns a failure as
expected, clicking the second link returns success… If this isn’t a
bug can someone explain to me why :slight_smile: – thanks

in my app/controllers/test_controller.rb…

def test
unless request.get?
id= params[:id]
if id == “1”
render :text => “login”, :layout => false, :status => 500
else
redirect_to :action => ‘index’
end
end
end

in my app/views/test/test.rhtml…

<%= link_to_remote “test_good”,
:update => “test_div”,
:url => { :controller => ‘test’ :action => ‘test’, :id => 1},
:loading => “alert(‘loading’)”,
:success => “alert(‘success’)”,
:failure => “alert(‘failed’)”,
:position => “after”
%>

<%= link_to_remote "test_bad", :update => "test_div", :url => { :controller => 'test', :action => 'test', :id => 2 }, :loading => "alert('loading')", :success => "alert('success')", :failure => "alert('failed')", :position => "after" %>

test

<div id= "test_div" this is text in a div

On Mar 27, 2006, at 9:46, Jim M. wrote:

When a controller responds to a link_to_remote with a redirect_to, the
link_to_remote gets a success callback, this would seem like a bug to
me, at a minimum it should return a failure?

This is driving me crazy because all the login engines/generators
respond to an authentication error with a redirect_to. The work around
is to change them to all do a

render :layout => false, :status => 500 if request.xhr?

Redirection in Ajax actions needs to use JavaScript, I have this in
application.rb:

Ajax actions that find some problem need to do a redirect, but

+redirect_to+

does not work well because we are being called from JavaScript

and is the

result of the redirection what will end up in the original view,

the main

page is still there. To get a regular redirection we need to

send back a

littel JavaScript.

def javascript_redirect_to(options={},
*parameters_for_method_reference)
url = url_for(options, *parameters_for_method_reference)
render :text => %Q{}
end

If an action uses redirect_to then it is not ready out-of-the-box for
Ajax usage, unless the redirection points to another Ajax action.

– fxn

Wow thanks that works incredibly well… With a minor mod I can plug
this in everywhere I do a redirect_to… Thank you!

def javascript_redirect_to(options={},
*parameters_for_method_reference)
if request.xhr?
url = url_for(options, *parameters_for_method_reference)
render :text => %Q{}
else
redirect_to options
end
end

Very good and simple indeed.

Still, the questions about redirect_to is still open. Should it not
result
in failure as http code is 302?

Emin H. wrote:

Very good and simple indeed.

Still, the questions about redirect_to is still open. Should it not
result
in failure as http code is 302?

Aha! Thank you, this has been driving me nuts for the last few days, I
was almost at the end of my tether.

I have to agree with the original sentiment. I would expect the redirect
to come back with a 302 and hence be caught by the :failure part of the
redirect_to.

Thanks for the answers to this question.

Jeff

It does seem to be a bug to me, but without really understanding how the
prototype library handles AJAX I can’t say for sure. But any unexpected
response should return an error not success IMHO.

On Mar 27, 2006, at 22:43, Jim M. wrote:

It does seem to be a bug to me, but without really understanding
how the
prototype library handles AJAX I can’t say for sure. But any
unexpected
response should return an error not success IMHO.

Why do you consider a redirect to be an unexpected response? Why
should a redirect be an error?

– fxn

Added an extra ‘do’ on there :wink: Should be:

render :update {|page| page.redirect_to(:controller =>
‘my_controller’, :action => ‘my_action’)}

On 3/27/06, Cody F. [email protected] wrote:

is to change them to all do a

result of the redirection what will end up in the original view,

http://www.codyfauser.com


Cody F.
http://www.codyfauser.com

You can also do the JavaScript redirect with RJS templates:

render :update do {|page| page.redirect_to(:controller =>
‘my_controller’, :action => ‘my_action’)}

Basically does the same thing, but is less messy.

On 3/27/06, Jim M. [email protected] wrote:

end

+redirect_to+
render :text => %Q{<script
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Cody F.
http://www.codyfauser.com

Basically the ajax handler is passing the redirect through as a success,
so the recipient goes ahead and thinks whatever it requested was
returned successfully, however a redirect (or any other response code
other than 2xx) would not be a success. I undertand it can’t do a
redirect as it needs to do it via javascript, but is it correct for it
to just pass the response through as success? and not flag the fact that
it got a non-ajax response from an ajax request.

I thought rjs was edge only but now I see you released a plugin, I can’t
reach your site right now (its probably busy giving everyone the plugin
;), but I’ll check that out as soon as I can get onto your site.

Then I’ll need to update the user engine to use them instead of straight
redirects.

thanks

Emin H. wrote:

Very good and simple indeed.

Still, the questions about redirect_to is still open. Should it not
result in failure as http code is 302?

No. Just as your browser follows a redirect, rather than treating it as
an error, an Ajax request can follow a redirect.

regards

Justin

Ahh but that is exactly what is not happening, if you run my little test
you will see that the ajax handler ignores the 302 response, does not
redirect, and quietly triggers the success callback.

If you do a render :status => 302 then it will catch the 302, but a
redirect_to which supplies a standard http 302 is ignored.

I understand why it doesn’t redirect now, but I don’t understand why it
is treated as a success.
If it can’t handle a standard redirect it should report it is an error
at least.

This causes all sorts of debugging problems when mixing the login
engines with ajax calls.

On 3/27/06, Jim M. [email protected] wrote:

I thought rjs was edge only but now I see you released a plugin, I can’t
reach your site right now (its probably busy giving everyone the plugin
;), but I’ll check that out as soon as I can get onto your site.

My awesome textdrive server crashed again and the cron script that
starts lighty on reboot somehow failed. I manually restarted it now,
though.

Also, with the release Rails 1.1 very soon the plugin won’t be
necessary anymore.

‘my_controller’, :action => ‘my_action’)}

      url = url_for(options, *parameters_for_method_reference)

the main
If an action uses redirect_to then it is not ready out-of-the-box for

Cody F.
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Cody F.
http://www.codyfauser.com