I write down the points that I got stuck in creating the portfolio as a memorandum. This time, we implemented a new user registration / login function in devise. There are two types of login users, corporate members and individual members, and I wanted to change the screen transition depending on the corporation or individual at the time of new registration and login, so I tried to change the redirect destination.
By default, devise specifies the redirect destination, so I had to customize the devise controller.
Ruby on Rails'6.0.0' Ruby'2.6.5'
The login function has been implemented using devise.
Terminal
% rails g devise:controllers companies
With the above command, you can edit the controller of the corporate devise. I think the file will be generated on the app/controllers/companies side.
If you check the routing with rails routes,
Terminal
new_company_session GET /companies/sign_in(.:format) devise/sessions#new
company_session POST /companies/sign_in(.:format) devise/sessions#create
(abridgement)
new_company_registration GET /companies/sign_up(.:format) devise/registrations#new
It will be displayed as above. Since it is not reflected in devise/sessions and devise/registration, customize it by routing.
config/routes.rb
devise_for :companies, controllers: {
sessions: 'companies/sessions',
registrations: 'companies/registrations'
}
If you customize and perform rails routes again, the notation will change as shown below.
Terminal
new_company_session GET /companies/sign_in(.:format) companies/sessions#new
company_session POST /companies/sign_in(.:format) companies/sessions#create
(abridgement)
new_company_registration GET /companies/sign_up(.:format) companies/registrations#new
From here, define a method that specifies the redirect destination.
app/controllers/companies/registrations_controller.rb
class Companies::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
before_action :configure_account_update_params, only: [:update]
def after_sign_up_path_for(_resource)
companies_articles_path(Redirected path)
end
(abridgement)
end
app/controllers/companies/sessions_controller.rb
class Companies::SessionsController < Devise::SessionsController
before_action :configure_sign_in_params, only: [:create]
def after_sign_in_path_for(_resource)
companies_articles_path(Redirected path)
end
(abridgement)
end
I was able to specify the redirect destination above! !! !! !!
Recommended Posts