dongdongxiao
dongdongxiao

Reputation: 169

how to create users_controller.rb with devise?

I used gem: devise to create models/user.rb by running rails g devise User, and I used devise to create views by running rails g devise:views. Now I want to create controllers/users_controller.rb. How can I do that?

rails g controller users

Is this correct? And is this user connected with devise User? I looked at another app, and class UsersController < ApplicationController works there.

I am new. Thanks for answering.

Upvotes: 1

Views: 2905

Answers (1)

Deepesh
Deepesh

Reputation: 6418

You can generate the devise controllers using its generate command:

rails generate devise:controllers [scope]

The scope means which folder you want to create it, like if it is for users then you can give users as scope which will generate the controller in app/controllers/users/.

You will need to specify this in routes so that rails uses your controllers:

devise_for :users, controllers: { registrations: "users/registrations" }

If you need a controller named users_controller then you can generate it using:

rails g controller users

But it will not connect with devise. All the actions (new, create, update, edit) of User are handled in registrations_controller by devise. So you can always override them if that is what required by you. So it will call your custom code instead of the default code.

If you create your own users_controller it will be normal rails controller. Just define routes for it and create the views inside the users folder it will work.

More info you can find in devise wiki:

https://github.com/plataformatec/devise#configuring-controllers

Hope this helps.

Upvotes: 5

Related Questions