设计authenticate_admin错误的重定向

时间:2014-04-04 08:32:35

标签: ruby-on-rails ruby-on-rails-4 devise rails-engines

我的应用程序带有User模型和以下路线:

devise_for :users, path: 'account',
  path_names: { sign_in: 'login', sign_out: 'logout' },
  controllers: { registrations: 'registrations' }

安装在“/ admin”的可安装引擎,带有AdminUser型号和以下路线:

devise_for :admin_users, class_name: "MyEngine::AdminUser", module: :devise,
  path: '', path_names: {sign_in: 'login', sign_out: 'logout'},
  controllers: { registrations: 'my_engine/registrations' }

在引擎应用程序控制器内部,before_filter :authenticate_admin_user!错误地重定向到 main_app根路径,而不是管理员登录(或任何登录)页面,给予:

Completed 401 Unauthorized

为什么这样做而不是要求admin_user登录?

2 个答案:

答案 0 :(得分:1)

我必须在main_app路由中添加设计路由,并稍微更改了登录/注销路径:

devise_for :admin_users, class_name: "MyEngine::AdminUser", module: :devise,
  path: '', path_names: {sign_in: 'admin/login', sign_out: 'admin/logout'},
  controllers: { registrations: 'my_engine/registrations' }

我仍然认为应该有一种方法可以在宝石内部完成所有工作,但现在这种方法很有用......

答案 1 :(得分:0)

你试过这个:

#config/routes.rb
devise_for :admin_users, class_name: "MyEngine::AdminUser", module: :devise,
  path: '', path_names: {sign_in: 'login', sign_out: 'logout'},
  controllers: { registrations: 'my_engine/registrations', sessions: "my_enging/sessions" }

#app/controllers/admin/sessions_controller.rb
class Admin::SessionsController < ::Devise::SessionsController

    protected

    #Logout Path
    def after_sign_out_path_for(resource_or_scope)
        admin_root_path
    end

    #Login Path (if already logged in)
    def after_sign_in_path_for(resource)
        admin_root_path
    end

end