Search code examples
ruby-on-railsauthenticationdevise

Remembering original destination after authentication with Devise


If a user accesses a protected page but is not signed in, I'd like to do the following -

  1. Redirect them to a "Log In" page with the original destination saved as a query parameter. e.g. /log-in?dest=%2Faccount%2Fsettings

  2. On successful log in, redirect to the originally intended page instead of the default root_path, in this case /account/settings

Is there an easy way to accomplish this using Devise? By default it doesn't seem to "remember" the original page after logging in so I'm guessing I'll have to override some before_action somewhere.

Thanks!


Solution

  • the devise gem has an awesome wiki where a lot of questions are answered in a tutorial style way. So for your second question, you can follow. this one:

    One tip for this one: in the "first approach" they mention that

    To redirect to the stored location after the user signs in you would override the after_sign_in_path_for method:

    You can define this method in the devise controllers if you generate them.

    Following this approcach you probably don't even need your first point.