当前位置: 动力学知识库 > 问答 > 编程问答 >

Rails, handling multiple redirects

问题描述:

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

网友答案:

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
分享给朋友:
您可能感兴趣的文章:
随机阅读: