Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise gem in Rails: generate user_root_path

Trying to redirect users to their associated 'home' page after successful login w/out nil'ing out stored_location_for(resource_or_scope)...which is giving me some endless redirect loops (pretty sure I've set it up wrong).

Regardless, I'm looking for a better approach...

Devise's docs state: After signing in a user, confirming the account or updating the 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. This means that you need to set the root inside your routes: root :to => "home"

I'm sorta a newbie...how does one go about generating this home_root_path for each user?

rDocs also mention:

-- (Object) after_sign_in_path_for(resource_or_scope)

The default url to be used after signing in. This is used by all Devise controllers and you can overwrite it in your ApplicationController to provide a custom hook for a custom resource. By default, it first tries to find a resource_root_path, otherwise it uses the root path. For a user scope, you can define the default url in the following way:

 map.user_root '/users', :controller => 'users' # creates user_root_path

 map.namespace :user do |user|
   user.root :controller => 'users' # creates user_root_path
 end

but these just gives me undefined local variable or methodmap' for #ActionDispatch::Routing::Mapper:…` errors.

like image 225
Meltemi Avatar asked Jan 20 '11 23:01

Meltemi


4 Answers

If you would like to redirect using a route in answer to your question below:

how does one go about generating this home_root_path for each user?

This will work if you place it in your config/routes file. It will redirect a user to articles#index after (for example) a successful confirmation.

    # Rails 4+
    get 'user_root' => 'articles#index', as: :user_root

    # Rails 3
    match 'user_root' => 'articles#index', as: :user_root

See Devise: Controllers Filters and Helpers

like image 145
SnapShot Avatar answered Oct 17 '22 08:10

SnapShot


You could try something like this:

application_controller.rb:

def after_sign_in_path_for(resource_or_scope)
  # return home_page_path for user using current_user method
end
like image 27
polarblau Avatar answered Oct 17 '22 10:10

polarblau


Dug around a bit to figure out the same thing. @polarblau's answer is correct,

def after_sign_in_path_for(resource_or_scope)
  user_info_path(current_user)
end

where user_info_path is the path to the page you wish to display.

Also, I would allow this to fall back to super just in case, although I'm not entirely sure if that is necessary...

def after_sign_in_path_for(resource)
  if resource.is_a(User)
    user_info_path(resource)
  else
    super
  end
end
like image 22
Rick Avatar answered Oct 17 '22 08:10

Rick


I spent several hours trying to get the same functionality, and this is the code that ended up working for me:

def after_sign_in_path_for(resource)
    current_user
end

If I ever tried current_user_path, I always got undefined local variable or method current_user_path errors.

Also, I'm using Rails 3.2.8 and Devise 2.1.2.

Hope that helps.

like image 40
Kyle Carlson Avatar answered Oct 17 '22 10:10

Kyle Carlson