我试图限制注册到Devise管理员。如果可能的话,我现在想避免使用CanCan。我已经创建了一个单独的设计管理模型,如选项#1中所述:https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role。如何使用Devise限制注册到管理员
接下来,我为用户设置了一个CRUD界面,如下所述:https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface。
我想在Users控制器中使用诸如before_filter :authenticate_admin!
之类的东西限制新注册,但由于某些原因,它不限制新注册。
我的routes.rb看起来是这样的:
devise_for :admins
devise_for :users, :path_prefix => 'd'
resources :admins
resources :users, :controller => "users"
任何想法,为什么before_filter :authenticate_admin!
不限制新注册?
当你添加一个管理员角色时,你应该能够使用'before_filter:authenticate_admin!'。目前,我正在为用户索引和显示操作(用于以管理员身份管理用户)为我工作。我不确定为什么它不能在创建操作上工作。我不想删除:可注册,因为我希望管理员能够添加用户。 – Scott 2012-04-05 01:17:59
嗨斯科特,你不需要定义一个新的'create'动作。如果你仔细看看devise的源代码,你可以在'app/controllers/devise/registrations_controller.rb'中找到devise为你做的一切,除了这些动作'authenticate_scope!''authenticate_admin! '在你的情况下,是前置的,只适用于**'编辑销毁更新**这三个动作。如果你真的想添加'authenticate_admin!'过滤器来创建动作,你可以继承'Devise :: RegistrationsController'并添加'prepend_before_filter:authenticate_scope !,:except => [:cancel]'。 – Tomato 2012-04-05 02:09:53
我认为这只是使用设计变得太复杂了,所以我添加了CanCan,这非常棒。谢谢您的帮助。 – Scott 2012-04-07 01:21:54