Ruby on rails 设计-仅由管理员创建用户

Ruby on rails 设计-仅由管理员创建用户,ruby-on-rails,devise,Ruby On Rails,Devise,我正在创建一个只需管理员即可创建新用户的应用程序: routes.rb: 用户\u controller.rb 当我打开 有什么问题吗?多谢各位问题在于你把应用程序的功能与设计功能混淆了: #config/routes.rb resources :users #-> nothing to do with devise 创建用户时,您使用的是designebuild\u资源helper。问题在于,这将需要设计功能,这对于用户\u控制器是不会发生的 要使用注册\u参数或构建资源,您必须确定到

我正在创建一个只需管理员即可创建新用户的应用程序:

routes.rb: 用户\u controller.rb 当我打开


有什么问题吗?多谢各位

问题在于你把应用程序的功能与
设计功能混淆了:

#config/routes.rb
resources :users #-> nothing to do with devise
创建用户时,您使用的是
designe
build\u资源
helper。问题在于,这将需要设计功能,这对于
用户\u控制器
是不会发生的

要使用
注册\u参数
构建资源
,您必须确定到
设计
控制器的路由范围(以便所有可用会话数据都在那里)

这样,您就可以用自己的代码覆盖标准的
designe::RegistrationController

#app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
   before_action :authenticate_user!
   before_action :check_admin

   def create
      build_resource(sign_up_params)
      ...
   end

   private

   def check_admin
      redirect_to root_path unless current_user.admin?
   end
end
#app/controllers/registrations_controller.rb
类注册控制器<设计::注册控制器
在\u操作之前:验证\u用户!
行动前:检查\u管理员
def创建
构建资源(注册参数)
...
结束
私有的
def检查管理
将\重定向到根\路径,除非当前\ u user.admin?
结束
结束
--

我建议您要么从
用户
控制器中删除
设计
功能,要么覆盖
注册
控制器,这样只有管理员才能创建用户(您似乎已经在尝试这样做了)

I got this error:

AbstractController::ActionNotFound at /users/new
Could not find devise mapping for path "/users/new".
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

  devise_scope :user do
    get "/some/route" => "some_devise_controller"
  end

2) You are testing a Devise controller bypassing the router.
   If so, you can explicitly tell Devise which mapping to use:

   @request.env["devise.mapping"] = Devise.mappings[:user]
#config/routes.rb
resources :users #-> nothing to do with devise
#config/routes.rb
devise_for :user, skip: [:registrations]
devise_scope :user do
   resources :users, path: "", only: [:new, :create], controller: "registrations" #-> url.com/users/new
end
#app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
   before_action :authenticate_user!
   before_action :check_admin

   def create
      build_resource(sign_up_params)
      ...
   end

   private

   def check_admin
      redirect_to root_path unless current_user.admin?
   end
end