Development Registration Registrar Extension

I have an Accounts model configured using Devise. Devise uses several attributes (e.g. email, password, etc.), but I have a few other attributes that I made. When registering, I would like to set them along the way.

How to renew the registration controller? I understand that I need to create a new controller as follows:

 class AccountsController < Devise::RegistrationController def create super end end 

Can I just add my code right after super ? I think it would be too late, since the resource would have already been saved. What is the best way to do this?

If I wrote writing from scratch, how would I know that I did not miss anything that Devise does?

Thanks,

+6
source share
3 answers

If you want to rewrite the controller from scratch for complete control, start with register_controller.rb Source code and make the necessary changes.

Saying that using your user controller is as simple as changing the route:

 devise_for :users, :controllers => { :registrations => "users/custom_controller" } 
+2
source

According to the Devise documentation, yes, just like Josh's answer, you have to change the controller. Although you do not have to start from scratch. Take a look at the documentation.

You can create a controller to add settings:

 Example: rails generate devise:controllers [scope] 

So, you can run the following for the "Your Users" area:

 rails generate devise:controllers users 

This gives you the controllers in the folder located here: application / controllers / users

Then tell the route file to use this controller. Update the development route as follows:

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

And finally, copy all kinds. If you have not studied the views, you need to do it. The controller has changed, so your views will also be required.

+9
source

If you need fields for information provided by the user, there is no need to expand the controller.
If you want to add them automatically, there is no reason not to do this in the model! (if it does not depend on the session or request)

In the first case, see https://github.com/plataformatec/devise#configuring-views

You must change the DEVise controller ONLY if you intend to change the registration flow.

0
source

Source: https://habr.com/ru/post/947315/


All Articles