1
votes

I'm using the devise gem with a rails 4.1.4 app, and needed to add some custom key-value pairs in a session variable right after a user has signed in.

I overrode the following methods

after_sign_in_path_for(resource)
after_sign_up_path_for(resource)
after_update_path_for(resource)
after_resetting_password_path_for(resource) methods as prescribed here:

https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

Yet, on signing in, I enter the after_sign_in_path_for multiple times, with the following output:

Started GET "/users/sign_in" for 127.0.0.1 at 2014-10-08 22:56:45 +0530 Processing by Devise::SessionsController#new as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Redirected to http://localhost:3000/users/sign_in Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 3ms (ActiveRecord: 0.6ms)

each time, before finally redirecting me to the root path. (The browser temporarily shows the redirect loop error message before redirecting me to the root).

EDIT:

So turns out that since I have 2 different devise models (users and admins), this line of code in after_sign_in_path_for:

sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')

is creating the url "http://localhost:3000/admins/sign_in" instead of "http://localhost:3000/users/sign_in"

How do I make url_for default to the users model and not the admin model?

Thanks!

1

1 Answers

0
votes

Ran into this issue myself. For whatever reason, url_for seems to default to the admin (no idea why) The good news is that you don't have to use url_for to accomplish this.

Instead, get rid of

sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')

and replace

if request.referer == sign_in_url

with

if request.referer == new_user_session_url

The helper makes your code cleaner and allow you to specify which devise model you want.