CRUD actions on a Devise User model - Rails 5 - ruby-on-rails

I'm working on a project that has a User Devise model and an Admin Devise model, and I want an Admin to be able to perform CRUD on the User model.
I've set up both Devise models following the Devise Wiki's How to Setup Multiple Devise User Models guide (including step 4 - exposing the scoped controllers).
This has given me the Devise views and controllers for confirmations, passwords, registrations, sessions and unlocks, but no users_controller to add CRUD actions to.
Could I simply create a users_controller and make sure it uses the correct users table in the database, or should I add CRUD actions to the registrations_controller?
Is the above advisable, or is there a more elegant way of setting up a CRUD interface for an Admin to be able to manage the User model?
Any help would be much appreciated.

Devise works only with sign_up/sign_in process. It assumes not only simple user creation, but some more things, like email sending.
So if you want to create/update/destroy users you need to create separate UsersController. It is better to add an admin namespace to it

Related

devise for two models

I am building an app with "nested authentication" That means that I have a House modle (with devise) and a house has many users and once inside the House authentication, I would like the users to be able to sign in as well. I have also added a User model with Devise. My question is that right now I am getting errors because I the devise sign_up form that I have designed for Houses doesn't work for the User model. How do I create separate sign_up and sign_in forms for two different user models with devise? Or is there a better way to go about doing this? Thanks!
How do I create separate sign_up and sign_in forms for two different user models with devise?
rails g devise:views
This will create an app/views/devise.
Then: set "config.scoped_views = true" inside "config/initializers/devise.rb".
From the README:
After doing so, you will be able to have views based on the role like "users/sessions/new" and "admins/sessions/new". If no view is found within the scope, Devise will use the default view at "devise/sessions/new".

How Do I Create a User Profile With Devise?

I really like how devise offers an easy to use registration system out of the box but I'm having trouble extending it to do what I need. I need to create a public user profile for each user that shows their information like name, email, bio, and more info. I've done this in the past before with a users/show function but since devise doesn't provide any easily editable controllers, I'm having trouble figuring out how to do this. I've already run rails generate devise:views to copy the devise views to my app but I don't know where to go from here. Any help would be much appreciated.
Sounds like you want users to update their profile at the same time they create their account? If so, you can setup an associated Profile model with the User model. Using accepts_nested_attributes_for you can then create a record for the nested model on devise user registration submit/creation.
Here's a great screencast covering nested models and I also suggest you search other devise relate SO posts as this question has been discussed before.
There is an alternative approach, that is simpler to implement — only allow registered users edit/update their profile. This way you don't have to alter the Devise views and you can setup the various CRUD actions via a separate non-devise controller.
Throw in an Access Control List (ACL) solution such as CanCan (there are other alternatives too!) and you can even allow other users view profiles but deny access to edit/destroy etc.

How to have two different login and logout path with Devise

I have one Devise model: User. I am needing two different login views, /login and /admin/login. Also, there would need to be two logout actions that redirect to different places (/logout and /admin/logout). Is this possible? Any pointers?
Thanks!
you can add all the views and controllers of devise to your project. rails generate devise:views This adds a devise folder inside your app/views. Inside the session/new view you can modify the loginform based on the route used. The same can be done for the destroying of sessions by overriding the controllers.
However, there might be something wrong in your logic if you need multiple routes to login and logout. If you simply need to elevate people to admin level or allow access to a admin like rails_admin there are plenty alternatives. You could for example have a look at CanCan and implement RBAC into your application.

Create, update and destroy actions in an UsersController that uses Devise for registration?

Should I implement the new, create, edit, update and destroy actions on a controller whose corresponding model uses Devise's :registerable module?
In other words, should I keep the CRUD interface even though Devise manages the registration process for me?
If you want to manage your Users (or whatever your Devise model is called) via a CRUD interface, you'll need to have those actions available. Unless I'm mistaken, if you manage your Users with a CRUD setup, you'll need to remove the :registerable from the model.
If you want need/want a CRUD interface for your Users, you can remove the CRUD actions (Devise will work fine without a CRUD interface, or CRUD actions in your controller).

Rails 3: Devise users_controller, where is it?

After I create a devise as Users, where is users_controllers.rb?
Devise doesn't create a controller along with your model, so you have to generate it yourself:
rails g controller Users
Note that Devise uses its own internal controllers for things like session management and registrations. You can extend these, if you need to, but it is not mandatory.

Resources