Redirect to specific URL after logging in
Chances are that your user is being redirected before after_sign_in_path
is called. This happens if the user tries to go to a page that is protected by authentication directly. This will happen all the time if you have your root_path
('/') protected by authentication.
There's a discussion on google groups about this topic:
The quick and dirty solution is to overwrite stored_location_for
to always return nil
like so:
class ApplicationController < ActionController::Base ... private def stored_location_for(resource_or_scope) nil end def after_sign_in_path_for(resource_or_scope) my_favorite_path endend
I think the after_sign_in_path_for
method in Devise is what you're looking for.
Define that method in your ApplicationController and it will over-ride Devise's default implementation. This is what the documentation specifies to do.
Details here: http://rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers:after_sign_in_path_for
Suppose you want to show user's dashboard after logging in.
class HomesController < ApplicationController def index if current_user //returns nil if not logged in @users = User.all render :dashboard end end def dashboard @users = User.all endend
in routes.rb:
root :to => 'homes#index'
If logged in, if-block is entered in index action and dashboard.erb is rendered.(make sure to initialize all variables, required by dashboard.erb, in your if-block)Otherwise rails renders index.erb