Back to Devise Wiki

How To: Redirect To A Specific Page On Successful Sign In

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

latest874 B
Original Source

config/routes.rb

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

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

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

For Rails 6, use:

ruby
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

ruby
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.