在命名空间内设计 [英] Devise within namespace
问题描述
我正在为正常用户和管理员的后端分割我的rails项目在前端。因此,我创建了一个命名空间admin,以便我可以轻松地在地图管理员中控制管理员特定的控制器方法/布局/身份验证。
我正在使用Devise注册/仅验证我的管理员。因为它只用于管理员只是我试图将Devise移动到管理命名空间。
我找不到我正在寻找的文档,但我在routes.rb中尝试过这样的事情:
命名空间'admin'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b 我还试图制作一个自定义的Devise :: Sessions控制器,但这似乎也没有解决。
有谁知道如何去做这个?我应该使用定制路线设计一个自定义(管理)布局?
解决方案只需将移动设计到管理员命名空间是错误的。 Devise使用像 Devise :: SessionsController
这样的控件,而不能被移动。
我通常创建自己的控制器并从Devise继承他们:
class Admin :: SessionsController< :: Devise :: SessionsController
布局admin
#其余的是继承的,所以它应该工作
end
并在 config / routes.rb
中配置:
devise_for:admins,:controllers => {:sessions => admin / sessions}
或只能更改布局,通过使布局更复杂一些:
class ApplicationController< ActionController :: Base
layout:layout
private
def layout
如果devise_controller? &安培;&安培; devise_mapping.name ==:admin
admin
else
application
end
end
end
I'm trying to split my rails project in a front-end for regular users and a back-end for admins. Therefore i have created a namespace 'admin' so that i can easily control admin specific controller methods/layouts/authentication in the map admin.
I'm using Devise to register/authenticate my admins only. Because it is only used for admins only i'm trying to move Devise to the admin namespace.
I could not find exactly what i was looking for in the documentation of Devise but i tried something like this in routes.rb:
namespace 'admin'do
devise_for :admins
end
I also tried to make a custom Devise::Sessions controller but that too didn't seem to work out.
Does anyone know how to do this? Should i just use the regular routes for devise with a custom(admin) layout?
解决方案 Simply "moving" Devise to the admin namespace is wrong. Devise uses controllers like Devise::SessionsController
and that cannot be "moved".
I usually create my own controllers and inherit them from Devise:
class Admin::SessionsController < ::Devise::SessionsController
layout "admin"
# the rest is inherited, so it should work
end
And configure this in config/routes.rb
:
devise_for :admins, :controllers => { :sessions => "admin/sessions" }
Or you could change the layout only, by making the layout a bit more complex:
class ApplicationController < ActionController::Base
layout :layout
private
def layout
if devise_controller? && devise_mapping.name == :admin
"admin"
else
"application"
end
end
end
这篇关于在命名空间内设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!