为每个控制器设计redirect_url
我想更改 Devise 在每个控制器上使用的redirect_url,即:
class DashboardController < ApplicationController
before_filter :authenticate_user!
end
class Admin::BaseController < ApplicationController
before_filter :authenticate_user!, :failure_url => admin_login_path
end
我不想更改管理员的授权方式。我不想创建管理模型 (devise_for :admin) 或客户故障应用程序。除了身份验证失败后的redirect_url 之外,我希望跨控制器具有相同的功能。
所有意见将不胜感激。
I would like to change the redirect_url that Devise uses on a per controller basis, i.e.:
class DashboardController < ApplicationController
before_filter :authenticate_user!
end
class Admin::BaseController < ApplicationController
before_filter :authenticate_user!, :failure_url => admin_login_path
end
I don't want to change how admins are authorized. I don't want to create an Admin model (devise_for :admin) or a customer failure app. I want the same functionality across controllers except for the redirect_url after an authentication failure.
All input would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用 CanCan 来实现
这里的 完整示例。 CanCan Wiki 中也对此进行了详细说明。祝你好运!
Have you considered using CanCan to achieve
Here's the full example. This is also detailed in the CanCan Wiki. Good luck!
我还没有测试过它,但似乎如果你在
class Devise::FailureApp
中修补一个名为redirect_url
的受保护方法,并使其根据某些内容返回一个 url逻辑,然后你可以重定向到其他 URL。问题是,FailureApp 与控制器完全解耦,因此您必须使用某些东西来传递此信息,例如带有控制器 ->failure_urls 映射的单例。I haven't tested it, but it seems that if you monkey patch a protected method called
redirect_url
inclass Devise::FailureApp
, and make it return an url based on some logic, then you can redirect to other URLs. The problem is, that the FailureApp is completely decoupled from your controller, so you'll have to use something to pass this information, such as singleton with controllers->failure_urls map.