Implement user registration function and corporate registration function separately in Rails devise

I've been learning programming for a month and a half.

I wanted to make a BtoC matching app by personal development, and realized that I had to register B and C for the time being, and even though I was a beginner, I implemented it while researching various things, so I would like to leave an article as an output. ..

Here, user and company generate and implement separate management tables.

1. Introduction of devise

First, write devise in Gemfile and bundle install.

Then install devise in the terminal as it is.

% rails g devise:install

2. Modify /config/initializers/devise.rb

There are various codes, but maybe line 247? (I was line 247) is commented out, Change the description that says config.scoped_views = false. Comment out is also removed as follows.

/config/initializers/devise.rb


  # ==> Scopes configuration
  # Turn scoped views on. Before rendering "sessions/new", it will first check for
  # "users/sessions/new". It's turned off by default because it's slower if you
  # are using only default views.
  config.scoped_views = true

3. Generate devise related model

This time, we will generate models for user and company.

% rails g devise user
% rails g devise company

4. Generate a controller related to each generated model

% rails g devise:controllers users
% rails g devise:controllers companies

5. Generate related view

% rails g devise:views users
% rails g devise:views companies

6. Routing settings

Now you can safely manage both user and company with devise. However, as it is, if you check the routing, there will be some overlap in the description of Controller # Action, so change it a little.

/config/routes.rb


  devise_for :companies, controllers: {
    sessions:      'companies/sessions',
    passwords:     'companies/passwords',
    registrations: 'companies/registrations'
  }
  devise_for :users, controllers: {
    sessions:      'users/sessions',
    passwords:     'users/passwords',
    registrations: 'users/registrations'
  }

This is OK.

7. Delete unused views

In the process of the procedure so far, a view file that is no longer used has been generated, so delete it.

% rails d devise:views

The implementation itself is completed above, but I was not sure about the description of strong parameters etc. at the time of new registration, so I will explain based on an example that I investigated and succeeded in my own way.

8. Set strong parameters

Until now, when there was one devise model, if there was an added column, it was set in application_controller.rb as follows.

/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  private
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
  end
end

However, since there are two models this time, I do not know how to set it, so I decided to set it with each controller related to each model generated by devise instead of application_controller.

Basically, I think it's enough to ** uncomment the relevant part and add an arbitrary column to the allowed key array **.

/controllers/users/registrations_controller.rb


class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]

  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :email, :password, :phone_number, :detail])
  end
end

/controllers/companies/registrations_controller.rb


class Companies::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]

  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:company_name, :email, :password, :phone_number, :office_url])
  end
end

that's all. With this, if you register the value in each view file, the data will be saved safely, and you can now implement functions such as login and logout.

Note that you must have two views of the form, user and company. Well, that's a partial template that doesn't matter.

Finally

Thank you for reading. Since I posted Qiita for the first time and I haven't understood the markdown notation in detail yet, it may have been a little difficult to read.

If you have any comments, please do not hesitate to contact us. We will use it for improvement from the next time onwards.

Recommended Posts

Implement user registration function and corporate registration function separately in Rails devise
Implement application function in Rails
[Rails] Implement User search function
[Rails] Implement credit card registration / deletion function in PAY.JP
Introduce devise in Rails to implement user management functionality
Implement user follow function in Rails (I use Ajax) ②
Implement user follow function in Rails (I use Ajax) ①
Implement login function in Rails simply by name and password (1)
Implement login function in Rails simply by name and password (2)
Implement simple login function in Rails
Implement CSV download function in Rails
Implement login function simply with name and password in Rails (3)
[Rails] Implementation of new registration function in wizard format using devise
[Rails] Function restrictions in devise (login / logout)
devise user registration
Create authentication function in Rails application using devise
Use [Rails] devise Guest user function (for portfolio)
Implement user edit / update function without using devise
Implement star rating function using Raty in Rails6
[Rails] Implement search function
Implement markdown in Rails
Implement post search function in Rails application (where method)
Try to implement tagging function using rails and js
[Rails6] devise + paranoia + both user logic deletion and unique constraints realized in MySQL8 series
Rails Posts and User Linkage
Implement LTI authentication in Rails
Implement import process in Rails
[Rails] Implement image posting function
[Rails] I tried to implement "Like function" using rails and js
Memorandum [Rails] User authentication Devise
[Rails] A simple way to implement a self-introduction function in your profile
I tried to implement Ajax processing of like function in Rails
[Implementation procedure] Create a user authentication function using sorcery in Rails
[Rails] Add a confirmation screen and a completion screen to devise membership registration.
Implement star evaluation function in Rails 6 (Webpacker is installed as standard)
[Rails] Session timeout setting in devise
Add a search function in Rails.
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[rails] Login screen implementation in devise
[Rails] Unexpected validation error in devise
[Rails] Implementation of user withdrawal function
Implement tagging function in form object
Implement PHP implode function in Java
[Rails] Comment function (registration / display / deletion)
Implement a contact form in Rails
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
Implement login, user registration, and two-factor authentication with Docker x Laravel 8 Jetstream
How to update user edits in Rails Devise without entering a password
[Rails 6] Register and log in with Devise + SNS authentication (multiple links allowed)
[Rails] How to get the user information currently logged in with devise
How to implement the email authentication function at the time of user registration
How to implement search functionality in Rails
Rails Addition of easy and easy login function
Implementation of user authentication function using devise (1)
Authentication function with Play Framework [Registration and authentication]
Implemented follow function in Rails (Ajax communication)
Implementation of user authentication function using devise (3)
How to implement ranking functionality in Rails
Implement button transitions using link_to in Rails
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise