简体   繁体   中英

Devise - Allow both individual and admin user creation

I have seen the few links on here regarding this topic but as yet have been unable to crack the problem. I want my application to allow users to register but also to register other users. So far my code lucks like so (though at this point I'm not sure I'm even on the right path and have probably included stuff which is not required just to make it work):

routes.rb

resources :devise
devise_for :users, path: 'devise'
devise_scope :user do
  get  'users/registrations/admin_new' => 'devise/registrations#admin_new'
  post 'users/registrations/admin_create' => 'devise/registrations#admin_create'
end

These are the two methods I have referenced in my route.

controllers/user/registration_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

  def admin_new
    puts "---------------"
    @user = User.new
  end

  def admin_create
    puts "---------------"   
  end
end

My view is as follows but shortened so as not to include all the fields:

views/devise/registrations/admin_new.html.haml

= simple_form_for(User.new, url: users_registrations_admin_create_path(User.new)) do |f|
  ...

This set up has gotten me to the point that the url:

http://localhost:3000/users/registrations/admin_new

loads the form alright however the puts I inserted does not appear in my console when it loads which is strange. Also i previously had the form as:

= simple_form_for(@user, url: users_registrations_admin_create_path(@user)) do |f|
...

however this resulted in the error:

undefined method `model_name' for nil:NilClass

which is why I changed to User.new instead of @user. When i submit the form it returns the error:

The action 'admin_create' could not be found for Devise::RegistrationsController

I'm at a loss as to why this is. I'm also unsure as to whether the structure of my devise is correct given the controllers are contained within a users folder while the views are contained with a devise folder. Any help on this would be greatly appreciated.

your route is problem.

post 'users/registrations/admin_create' => 'devise/registrations#admin_create'

which means this route to admin_create action in Devise::RegistrationsController

so router find a action in Devise::RegistrationsController but action "admin_create" exists in Users::RegistrationsController,

change

'devise/registrations#admin_create'
=> 'user/registrations#admin_create'

update1 I check your route out.

assigned route path to controller in 'controllers/ devise /registrations_controller.rb'

post 'users/registrations/admin_create' => 'devise/registrations#admin_create'

PREFIX users_registrations_admin_create
URL PATTERN /users/registrations/admin_create(.:format) 
Controller&Action devise/registrations#admin_create

assigned route path to controller in 'controllers/ user /registrations_controller.rb'

post 'users/registrations/admin_create' => 'user/registrations#admin_create'

PREFIX users_registrations_admin_create
URL PATTERN /users/registrations/admin_create(.:format) 
Controller&Action user/registrations#admin_create

only 'devise' controller name changed to 'user' in route, then fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM