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.
First, write devise in Gemfile and bundle install.
Then install devise in the terminal as it is.
% rails g devise:install
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
This time, we will generate models for user and company.
% rails g devise user
% rails g devise company
% rails g devise:controllers users
% rails g devise:controllers companies
% rails g devise:views users
% rails g devise:views companies
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.
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.
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.
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