[RUBY] [Rails 6] Change redirect destination at the time of new registration / login by devise

Introduction

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.

environment

Ruby on Rails'6.0.0' Ruby'2.6.5'

Premise

The login function has been implemented using devise.

① Create controller

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.

② Routing settings

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

③ Edit controller

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

[Rails 6] Change redirect destination at the time of new registration / login by devise
Email sending function with Action Mailer at the time of new registration
[Rails] Cancel / change the default password validation of devise
[Rails] Register by attribute of the same model using Devise
[Rails] When the layout change of devise is not reflected
[Rails] Implementation of new registration function in wizard format using devise
[Ruby on Rails] Change the save destination of gem refile * Note
[Rails 6] Implementation of new registration function by SNS authentication (Facebook, Google)
[Rails] About Uglifier :: Error: Unexpected token: at the time of deployment
[Rails] Change the label name of f.label
How to implement the email authentication function at the time of user registration
Change the save destination of the image to S3 in the Rails app. Part 2
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
[Rails] Get the path name of the URL before the transition and change the link destination
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
[Rails] How to decide the destination by "rails routes"
[Note] Summary of rails login function using devise ①
Environment construction method and troubleshooter at the time of joint development (rails, docker and github)
A review of the code used by rails beginners
Get the URL of the HTTP redirect destination in Java
Implementation of Ruby on Rails login function (devise edition)
Speed comparison at the time of generation at the time of date conversion
[Rails] How to change the column name of the table
[Rails] Initial setting of user-created login with devise, devise_token_auth
Get the URL of the HTTP redirect destination in Ruby
[Enum] Let's improve the readability of data by using rails enum
Get the object name of the instance created by the new operator
[Android] Exit the activity of the transition source at the time of screen transition
Change the file name and output destination of the JavaVM error file
[Rails] How to display the list of posts by category
[Rails] Create new files required for the application at once
About the behavior at the time of error of Files.copy (pathA, pathB)
[Rails] Get access_token at the time of Twitter authentication with Sorcery and save it in DB
[Spring Data JPA] Custom ID is assigned in a unique sequence at the time of registration.