为每个控制器设计redirect_url

发布于 2024-11-04 07:29:46 字数 419 浏览 1 评论 0原文

我想更改 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 技术交流群。

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

发布评论

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

评论(2

溇涏 2024-11-11 07:29:51

您是否考虑过使用 CanCan 来实现

rescue_from CanCan::AccessDenied do |exception|
  flash[:error] = exception.message
  redirect_to root_url
end

这里的 完整示例CanCan Wiki 中也对此进行了详细说明。祝你好运!

Have you considered using CanCan to achieve

rescue_from CanCan::AccessDenied do |exception|
  flash[:error] = exception.message
  redirect_to root_url
end

Here's the full example. This is also detailed in the CanCan Wiki. Good luck!

别把无礼当个性 2024-11-11 07:29:48

我还没有测试过它,但似乎如果你在 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 in class 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.

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