如何使用Devise将注册限制为管理员

Sco*_*ott 3 ruby ruby-on-rails devise

我正在尝试将注册限制为Devise管理员.如果可能的话,我现在想避免使用CanCan.:我已经在选项#1这里所描述创建一个独立的设计管理模式https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role.

接下来,我为用户设置了一个CRUD界面,如下所述:https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface .

我想通过使用类似于before_filter :authenticate_admin!用户控制器的东西限制新的注册,但由于某种原因,它不限制新的注册.

我的routes.rb看起来像这样:

devise_for :admins
devise_for :users, :path_prefix => 'd'
resources :admins
resources :users, :controller => "users"
Run Code Online (Sandbox Code Playgroud)

任何想法为什么before_filter :authenticate_admin!不限制新的注册?

小智 9

您无法before_filter :authenticate_admin!在用户控制器中使用,因为管理员和用户是您应用中的两个不同型号.

我不知道我是否完全明白你的意思,但如果你不想接受用户(或管理员)的新注册,你可以这样做:

# in your User(Admin) model
devise :registerable # remove :registerable
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!