Skip to content

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

natronic edited this page May 9, 2020 · 15 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

For Rails 6, use:

get '/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.

Clone this wiki locally