5
votes

In my rails app, I'm using devise for user registration. Now besides the user registrations, admins should create, edit and delete user in the backend, too. I have a problem with creating new users through that admin backend.

When I call the UsersController#new action in my browser it opens up the user entry form created by me. When I click on the submit button the Devise::RegistrationsController#create is called but it should call my UsersController#create action.

How do I achvieve to call UsersController#create when using the user creation in the admin backend, and to call Devise::RegistrationsController#create when a user uses the registration?

2

2 Answers

5
votes

1) add the path prefix to devise: devise_for :users, :path_prefix => 'd'

2) run rake routes:

user_registration POST   /d/users(.:format)   devise/registrations#create
...
users POST   /users(.:format)  users#create

So, the first route for Devise::RegistrationsController, second for your UsersController.

And you can simply use in the admin/new_user.html.erb: form_for User.new

0
votes

On the back of the Mikhail's answer, you can also tell devise to use any other pathname instead of 'users'. For example:

devise_for :users, path: 'auth'

would result in devise routes like /auth/sign_up, /auth/sign_in, etc. And your custom user routes would be safe.

(The full list of devise_for options can be found here)