2

我有一个程序,其中一个 gem,facebooker,调用一个重定向,在同一个动作中,我最终通过redirect_back_or_default. 我的问题是:

  1. 有没有办法捕捉多个重定向错误?开始/救援块似乎没有这样做。
  2. 或者,有没有办法检查是否已经调用了重定向,所以我不调用下一个?

在这一点上,我不想修改facebooker gem,那么你觉得最好的处理方法是什么?

谢谢大家,贾斯汀

4

1 回答 1

2

看看ActionController#redirect_to的来源会有所帮助:

raise AbstractController::DoubleRenderError if response_body

你可以像这样拯救异常(并且只留下日志行):

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

或者您可以测试(在我看来这不是一个好的做法) response_body 类似于 ActionController 所做的:

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

   redirect_to '/tester/index' unless response_body
  end    
end
于 2011-03-26T23:23:44.077 回答