Skip to content

How To: Redirect to a specific page on successful sign in out

josephers edited this page Nov 30, 2011 · 17 revisions

config/routes.rb

namespace :user do
    root :to => "welcome#index"
end

This does not work with Rails 3.0.7 in production mode. Use this instead:

match '/user' => "welcome#index", :as => :user_root

If you are using a gem that reads to URI for certain processing (like simple-navigation) the new URI will be '/users' even though you are at the '/welcome' URI (highlight is lost in simple-navigation and submenus are not rendered).

So the solution can be achieved with this:

app/controllers/application_controller.rb

def after_sign_in_path_for(resource)
	stored_location_for(resource) || welcome_path
end

To make the above work, the root path has to be publicly visible!

The resulting URI will be '/welcome', which can be properly processed.

After signing out

This works a lot like How To: Redirect to a specific page on successful sign in, except you use the method:

def after_sign_out_path_for(resource_or_scope)
  # logic here
end
Clone this wiki locally