[RUBY] [For rails beginners] Specify transition destination after logging in to multiple Devise models

When creating an administrator (admin), customer side (customer / user, etc.), I want to specify the redirect destination after each login.

Premise

-There are admin model and customer model as devise usage models (please replace the model name)

Implementation

application_controller.erb


class ApplicationController < ActionController::Base

  def after_sign_in_path_for(resource)
    case resource
    when Admin
      admin_top_path          #For path, specify the path to the transition destination you want to set.
    when Customer
      root_path              #Feel free to change the path here as well.
    end
  end

end

It's easy to write and it seems to be easy to remember. Let me explain a little about the description! -`After_sign_in_path_for (resource)` Argument (resource) information is used for conditional branching. -Processing is divided by case statement depending on whether the content of the resume instance is Admin or Customer. If you look up so far, where does resource come from and why is it named like this? I wondered if @user could be used, so I looked it up. (Please see below only for those who are interested)

bonus

ruby:registrations/new.html.erb


<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: new_customer_registration_path(resource_name)) do |f| %>
#Omitted below

The above code is one of the View pages that is automatically generated when using Devise. I did some research. ↓

-Devise is a framework that can handle authentication for multiple Models at the same time, so it seems that it is defined so that it does not matter what instance of Model comes. It seems that inside Devise, it is unified so that it can be referred to by the name ** resource **. Similarly, information about which Model you are trying to authenticate seems to be available under the name ** resource_name **. -Although ** (resource_name) ** is included in the url, it seems that ** (resource_name) ** is required because it is not possible to know which model is registered with just new_customer_registration_path.

It may have been a little unsatisfactory, but it was a good opportunity to dig deeper into the parts that were usually too convenient to implement without thinking. I am a newcomer with several months of programming experience, so please point out any strange parts ,!

Recommended Posts

[For rails beginners] Specify transition destination after logging in to multiple Devise models
(For beginners) [Rails] Install Devise
[Rails] Manage multiple models using devise gem
[For beginners] How to debug in Eclipse
[Rails] Add page nation to table [For beginners]
[Rails] How to implement unit tests for models
[Validation] rails How to specify after today's date
[Rails] Added in devise: username not added to database
[rails devise] How to transition users who are not logged in to the login page
I want to change the path after new registration after logging in with multiple devises.
How to implement login request processing (Rails / for beginners)
[Rails] Return login result in JSON format (for beginners)
[For beginners] How to operate Stream API after Java 8
[For Rails beginners] Implemented multiple search function without Gem
[For beginners] Minimum sample to display RecyclerView in Java
Beginners use ubuntu in windows to prepare rails environment
[Rails] Processing after adding a column to the devise table
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
Tutorial to create a blog with Rails for beginners Part 1
Establish a search bar in Rails ~ Join multiple tables to search
Super easy in 2 steps! How to install devise! !! (rails 5 version)
Options for rails new and settings to be done after rails new
Tutorial to create a blog with Rails for beginners Part 2
Tutorial to create a blog with Rails for beginners Part 0
[Rails] How to display error messages for comment function (for beginners)