Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

devise method after_sign_in_path_for doesnt work

I need to control redirecting after user is logged in. I found many answers to this problem point to after_sign_in_path_for method. So i added this to my application_controller.rb:

def after_sign_in_path_for(resource_or_scope)
    raise "SIGN IN TEST"
    dashboard_path
end

to check out how this works. But whenever i log in, there are no exceptions raised, method is just ignored. What shall I do to make devise use it?

EDIT as requested, fragment of routes.rb

devise_for :users, :controllers => {
    :sessions => "user/sessions",
    :registrations => "user/registrations",
    :passwords => 'user/passwords'
  }
like image 393
Leo Avatar asked Dec 17 '25 11:12

Leo


1 Answers

The official docs for this are here

--

We use this successfully like this:

#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
    def after_sign_in_path_for(resource)
        dashboard_path
    end
end

--

If your method is not being called, you'll want to firstly remove the RAISE call and try again by restarting your server. If this does not work, the issue will likely be with how Devise is working on your system

If you could try removing RAISE & letting us know the result, it will be a great help!


Update

EVEN BETTER that you're using users/sessions controller. Here's ours:

class Users::SessionsController < Devise::SessionsController

    ####################
    #    Redirects     #
    ####################

    protected

    #Login Path (if already logged in)
    def after_sign_in_path_for(resource)
        dashboard_path
    end

    #Logout Path
    def after_sign_out_path_for(resource_or_scope)
        root_url
    end

end
like image 197
Richard Peck Avatar answered Dec 19 '25 07:12

Richard Peck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!