当用户打开多个窗口时,使用Devise在Rails中捕获401错误

场景是这样的:用户有2个窗口,他/她登录。他/她从一个登录,保持登录到另一个,然后在后者,触发一些动作,比如表单提交。

最初,发生的是它抛出了无效的真实性令牌错误。 我已经将protect_from_forgery中的protect_from_forgery更新为protect_from_forgery with: :null_session这样它就会抛出401而不是Exception。

大! 现在对于第2步,而不是用户只是看到一行文字说You need to sign in or sign up before continuing. ,我想将他/她重定向到登录页面。

这就是我遇到问题的地方……我一直在阅读本指南: http : //agileleague.com/blog/rails-3-2-custom-error-pages-the-exceptions_app-and-testing-with -capybara /表示401错误不是默认由rails捕获的。 该指南有两行代码,用于定义和捕获它,然后路由中的一行代码将使路由工作。 基本上它看起来像这样:

 # add to app/controllers/application_controller.rb class UnauthorizedException  :unauthorized) # add to routes (in my case this is what I've done for Devise) devise_scope :user do match '/401', to: 'users/sessions#new', type: "401", via: :get end 

现在我对Exceptions处理非常环保,但对我来说这看起来不完整……而且它也无法正常工作。 如果我导航到本地服务器中的/401 ,我会立即进入登录页面,路由工作正常。 但是,如果我复制上面概述的方案,首先导致401,而不是被重定向,我仍然只剩下一个有一行纯文本的页面。

救命?