Rails,处理多个重定向

发布于 2024-08-25 22:08:09 字数 272 浏览 9 评论 0原文

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

  1. 有没有办法捕获多重重定向错误?开始/救援块似乎没有做到这一点。
  2. 或者,有没有一种方法可以检查重定向是否已被调用,这样我就不会调用下一个?

此时,我不想修改 facebooker gem,那么您认为处理此问题的最佳方法是什么?

谢谢大家, 贾斯汀

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-09-01 22:08:09

查看 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文