views:

220

answers:

2

Is there a way in Devise 1.0, the library for Rails 2.3, to redirect to a specific URL and not root_url after logging in?

EDIT: forgot to mention it's Devise 1.0

+1  A: 

I think the after_sign_in_path_for method in Devise is what you're looking for.

Define that method in your ApplicationController and it will over-ride Devise's default implementation. This is what the documentation specifies to do.

Details here: http://rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers:after_sign_in_path_for

Sidane
I've forgot to mention in Devise 1.0, not 1.1.
J. Pablo Fernández
after_sign_in_path_for is not being called on 1.0.8 at all.
J. Pablo Fernández
Looking at Devise's SessionsController, when login is successful, sign_in_and_redirect is called. This redirects to the previous location stored in the session. If no location is stored, then after_sign_in_path_for is called.Is the user logging in from (or via) the homepage? That could explain why it always redirects back there.
Sidane
Then, why didn't adding raise to that method didn't cause it to stop the request?
J. Pablo Fernández
Which method did you add raise to - sign_in_and_redirect? Are you using the default Devise controllers/views or have you overwritten any parts of them?
Sidane
+1  A: 

Chances are that your user is being redirected before after_sign_in_path is called. This happens if the user tries to go to a page that is protected by authentication directly. This will happen all the time if you have your root_path ('/') protected by authentication.

There's a discussion on google groups about this topic:

The quick and dirty solution is to overwrite stored_location_for to always return nil like so:

class ApplicationController < ActionController::Base  
...

  private 
  def stored_location_for(resource_or_scope)
    nil
  end

  def after_sign_in_path_for(resource_or_scope)
    my_favorite_path
  end
end
bowsersenior