2

I have a program where a gem, facebooker, calls a redirect and in the same action I end up callling a redirect through redirect_back_or_default. My question is:

  1. Is there a way to catch the multiple redirect error? A begin/rescue block doesn't seem to do it.
  2. Or, is there a way to check to see if a redirect has already been called so I don't call the next one?

At this point, I don't want to modify the facebooker gem, so what do you feel is the best way to handle this?

Thanks all, Justin

Justin
  • 11,483
  • 3
  • 16
  • 7

1 Answers1

2

A look at the source of ActionController#redirect_to helps out:

raise AbstractController::DoubleRenderError if response_body

You could rescue the Exception like this (and just leave the log line out):

class TesterController < ApplicationController
  #I am redirecting ever to index.html
  def index
   redirect_to '/index.html'

   redirect_to '/tester/index'
  rescue AbstractController::DoubleRenderError
   Rails.logger.info "I redirected two times at least but the user doesn't know"
  end
end

or you can test (in my opinion this is no good practice) for response_body similar to what the ActionController does:

class TesterController < ApplicationController
  def index
   redirect_to '/index.html'

   redirect_to '/tester/index' unless response_body
  end    
end
scoudert
  • 189
  • 2
  • 9
abstraktor
  • 955
  • 9
  • 20