diff --git a/README.rdoc b/README.rdoc index 3b043d9a..06c747a8 100644 --- a/README.rdoc +++ b/README.rdoc @@ -134,6 +134,8 @@ You have also access to the session for this scope: user_session +After signing in a user, confirming it's account or updating it's password, devise will look for a scoped root path to redirect. Example: For a :user resource, it will use user_root_path if it exists, otherwise default root_path will be used. + Devise let's you setup as many roles as you want, so let's say you already have this User model and also want an Admin model with the same authentication stuff, but not confirmation or password recovery. Just follow the same steps: # Create a migration with the required fields diff --git a/app/controllers/confirmations_controller.rb b/app/controllers/confirmations_controller.rb index 3ef3a69a..7245101c 100644 --- a/app/controllers/confirmations_controller.rb +++ b/app/controllers/confirmations_controller.rb @@ -24,7 +24,7 @@ class ConfirmationsController < ApplicationController if resource.errors.empty? sign_in(resource_name, resource) set_flash_message :success, :confirmed - redirect_to root_path + redirect_to home_or_root_path else render :new end diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb index 85bbe06c..3080cae9 100644 --- a/app/controllers/passwords_controller.rb +++ b/app/controllers/passwords_controller.rb @@ -30,7 +30,7 @@ class PasswordsController < ApplicationController if resource.errors.empty? sign_in(resource_name, resource) set_flash_message :success, :updated - redirect_to root_path + redirect_to home_or_root_path else render :edit end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index c6bf1b98..69ec1d24 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -32,8 +32,4 @@ class SessionsController < ApplicationController :scope => [:devise, :sessions], :default => :unauthenticated) end - def home_or_root_path - home_path = :"#{resource_name}_home_path" - respond_to?(home_path, true) ? send(home_path) : root_path - end end diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index 25a63e3b..3e344546 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -42,6 +42,21 @@ module Devise session[:"#{resource_name}.return_to"] = nil end + # Checks for the existence of the resource root path. If it exists, + # returns it, otherwise returns the default root_path. + # Used after authenticating a user, confirming it's account or updating + # it's password, so we are able to redirect to scoped root paths. + # Examples (for a user scope): + # map.user_root '/users', :controller => 'users' # creates user_root_path + # + # map.namespace :users do |users| + # users.root # creates user_root_path + # end + def home_or_root_path + home_path = :"#{resource_name}_root_path" + respond_to?(home_path, true) ? send(home_path) : root_path + end + # Attempt to find the mapped route for devise based on request path def devise_mapping @devise_mapping ||= Devise.find_mapping_by_path(request.path) diff --git a/test/rails_app/config/routes.rb b/test/rails_app/config/routes.rb index ab7471dd..34c41c95 100644 --- a/test/rails_app/config/routes.rb +++ b/test/rails_app/config/routes.rb @@ -11,7 +11,7 @@ ActionController::Routing::Routes.draw do |map| map.root :controller => :home map.connect '/admin_area/password/new', :controller => "passwords", :action => "new" - map.admin_home '/admin_area/home', :controller => "admins", :action => "index" + map.admin_root '/admin_area/home', :controller => "admins", :action => "index" map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'