Forum: Ruby on Rails Is this a bug in Ajax handling?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C31c7255fa4488dfc5d766403b497a36?d=identicon&s=25 Jim Morris (Guest)
on 2006-03-27 09:47
(Received via mailing list)
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 :) -- 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"
    %>
<p>
  <%= 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"
    %>

<h2> test </h2>

<div id= "test_div"
this is text in a div
</div>
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-03-27 10:45
(Received via mailing list)
On Mar 27, 2006, at 9:46, Jim Morris 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{<script type="javascript">window.location="#
{url}"</script>}
   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
C31c7255fa4488dfc5d766403b497a36?d=identicon&s=25 Jim Morris (Guest)
on 2006-03-27 11:05
(Received via mailing list)
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{<script
type="javascript">window.location="#{url}"</script>}
      else
          redirect_to options
      end
  end
7cda78d70ab775710f6fa24e0a5b68db?d=identicon&s=25 Emin Hasanov (Guest)
on 2006-03-27 13:48
(Received via mailing list)
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?
C728d93eb5205cf20cac0c2928b0ad62?d=identicon&s=25 Jeff Jones (rurounijones)
on 2006-03-27 15:21
Emin Hasanov 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
C31c7255fa4488dfc5d766403b497a36?d=identicon&s=25 Jim Morris (Guest)
on 2006-03-27 22:44
(Received via mailing list)
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.
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-03-27 22:58
(Received via mailing list)
On Mar 27, 2006, at 22:43, Jim Morris 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
2ccb2f7699267d258faaafce4ee997df?d=identicon&s=25 Cody Fauser (Guest)
on 2006-03-27 23:59
(Received via mailing list)
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 Morris <morris@wolfman.com> wrote:
>   end
> >>
> > +redirect_to+
> >     render :text => %Q{<script
> > 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
>


--
Cody Fauser
http://www.codyfauser.com
2ccb2f7699267d258faaafce4ee997df?d=identicon&s=25 Cody Fauser (Guest)
on 2006-03-28 00:01
(Received via mailing list)
Added an extra 'do' on there ;)  Should be:

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


On 3/27/06, Cody Fauser <codyfauser@gmail.com> 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 Fauser
http://www.codyfauser.com
C31c7255fa4488dfc5d766403b497a36?d=identicon&s=25 Jim Morris (Guest)
on 2006-03-28 01:09
(Received via mailing list)
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.
C31c7255fa4488dfc5d766403b497a36?d=identicon&s=25 Jim Morris (Guest)
on 2006-03-28 01:15
(Received via mailing list)
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
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-28 01:36
(Received via mailing list)
Emin Hasanov 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
C31c7255fa4488dfc5d766403b497a36?d=identicon&s=25 Jim Morris (Guest)
on 2006-03-28 03:00
(Received via mailing list)
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.
2ccb2f7699267d258faaafce4ee997df?d=identicon&s=25 Cody Fauser (Guest)
on 2006-03-28 04:55
(Received via mailing list)
On 3/27/06, Jim Morris <morris@wolfman.com> 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 Fauser
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Cody Fauser
http://www.codyfauser.com
This topic is locked and can not be replied to.