stored_location_for vs after_sign_in_path_for

1,264 views
Skip to first unread message

fearless_fool

unread,
Jun 1, 2011, 1:30:29 PM6/1/11
to Devise
In Devise 1.2.1, I've seen plenty of documentation on how to use
after_sign_in_path_for, but not much about stored_location_for. After
looking over the sources, I think I want to use stored_location_for.
Here's how the devise code uses them:

def redirect_location(scope, resource) #:nodoc:
stored_location_for(scope) || after_sign_in_path_for(resource)
end

If our resource is named 'user', then I should do:

session[:user_return_to] = my_special_path

Then the next time the user logs in, he or she will be directed to
my_special_path after a successful login. After that, the session
variable will be deleted and subsequent logins will use
after_sign_in_path_for(user) instead.

Question 1: is this correct?

Question 1a: is there no API for setting the session variable?

Question 2: does this hold true if the user registers as well? That
is, after a successful registration, the user is automatically logged
in (right?), he or she will be directed to my_special_path, right?

Question 3: Did I overlook something, or is this a hole in the
documentation?

I'll go ahead and test this in my code, but would love some assurance
that I'm headed in the right direction!

Walter Davis

unread,
Jun 1, 2011, 2:38:15 PM6/1/11
to plataforma...@googlegroups.com

On Jun 1, 2011, at 1:30 PM, fearless_fool wrote:

> In Devise 1.2.1, I've seen plenty of documentation on how to use
> after_sign_in_path_for, but not much about stored_location_for. After
> looking over the sources, I think I want to use stored_location_for.
> Here's how the devise code uses them:
>
> def redirect_location(scope, resource) #:nodoc:
> stored_location_for(scope) || after_sign_in_path_for(resource)
> end
>
> If our resource is named 'user', then I should do:
>
> session[:user_return_to] = my_special_path
>
> Then the next time the user logs in, he or she will be directed to
> my_special_path after a successful login. After that, the session
> variable will be deleted and subsequent logins will use
> after_sign_in_path_for(user) instead.
>
> Question 1: is this correct?
>
> Question 1a: is there no API for setting the session variable?
>
> Question 2: does this hold true if the user registers as well? That
> is, after a successful registration, the user is automatically logged
> in (right?), he or she will be directed to my_special_path, right?

In my limited experience, this happens when the user is first signed
in -- when they fill in their password after confirmation, etc. The
next page they hit will be the result of this method. By default, it
will either be their last-visited private page, if they were forced
into the registration funnel by landing on a protected page, or the
site root.

I have used it in a couple of applications to request that a user add
additional details to their profile that I don't bother to capture the
first time around. Sort of a staggered registration, in other words.

No idea about your other questions, though.

Walter

fearless_fool

unread,
Jun 1, 2011, 5:41:03 PM6/1/11
to Devise
So I've added the following to my code:

session[:user_return_to] = my_special_path

... and it sends the user to my_special_path after logging in *and*
after registering -- that's exactly the behavior I was looking for.

So my "question 3" remains: am I overlooking some documentation
somewhere?

If the answer is no, then I'll create a Wiki "how to" entry.

Carlos Antonio

unread,
Jun 1, 2011, 10:25:13 PM6/1/11
to plataforma...@googlegroups.com
Hello, you're right, it's supposed to work exactly this way. the :user_return_to session key (based on your scope) is the value Devise stores internally inside FailureApp to redirect you back later, after sign in / up.

About the docs, I think they could be expanded a bit more, so a wiki entry would help a lot =).

-- 
At.
Carlos A. da Silva

Reply all
Reply to author
Forward
0 new messages