-There are admin model and customer model as devise usage models (please replace the model name)
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)
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