If we are using Devise gem with our Rails project, One of the common tasks that a developer wants to do is to show a custom page when a user signed in .
So here our intention is to modify the default Devise's behaviour when a user signed in .
Let's consider the following code in application_controller.rb.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
def after_sign_in_path_for(current_user)
profile_path
end
end
Here is the routes.rb file :
Rails.application.routes.draw do
get 'profile' => 'users#profile'
end
Here after_sign_in_path_for method simply renders profile page after a successful login. Similarly after_sign_out_path_for method renders the intended custom page after a successful log out. Below are the excerpts from the application_controller.rb and config/routes.rb respectively:
def after_sign_out_path_for(resource_or_scope)
root_path
end
root 'homes#index'
So ,here after log out a user will be on the root page of the application.
One most important thing is after_sign_out_path_for method accepts a symbol with the scope, and not the resource .
0 Comment(s)