Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • after_sign_in_path_for method and after_sign_out_path_for method in rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.33k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: