5261: When redirecting during an AJAX request, send the target URI in
authorTom Clegg <tom@curoverse.com>
Thu, 5 Mar 2015 19:07:52 +0000 (14:07 -0500)
committerTom Clegg <tom@curoverse.com>
Thu, 5 Mar 2015 19:07:52 +0000 (14:07 -0500)
a JSON object {"href":"..."} instead of responding 302.

This lets us use "redirect_to X" to mean "send the user to page X"
regardless of whether the request is an XHR. Without it, client-side
code never sees the 302 at all: the browser handles the redirect
transparently, and the client-side code typically ends up trying to
parse HTML content as JSON.

apps/workbench/app/controllers/application_controller.rb

index b52591bc0caa5ff1b14da6ac7485a829eaeab1d8..0a13fa6b601d2d45a8eac3d0a18d0b2c951584e6 100644 (file)
@@ -267,6 +267,14 @@ class ApplicationController < ActionController::Base
     end
   end
 
+  def redirect_to uri, *args
+    if request.xhr?
+      render json: {href: uri}
+    else
+      super
+    end
+  end
+
   def choose
     params[:limit] ||= 40
     respond_to do |f|