I'd like to set a session variable once a user signs in based on a certain field in the User table. I don't want to have to create a custom Devise controller if I don't have to. Is there a way? Or will I have to go the custom controller route?
Asked
Active
Viewed 1.1k times
2 Answers
15
There is a callback after_sign_in_path_for, you can add it in your ApplicationController
protected
def after_sign_in_path_for(resource)
session[:domain_prefix] = current_user.domain_prefix
user_path(resource)
end
Dont forget return the path in the last line of method, otherwise the callback will redirect the request to content of session[:domain_prefix]
kkurian
- 3,844
- 3
- 30
- 49
Shairon Toledo
- 2,024
- 16
- 18
-
He is asking for adding a new session variable on login for ex: keeping user's name in session. {My example is totally useless but i think this is what he wants} – Mohit Jain Sep 14 '11 at 21:09
-
Thanks, this should definitely work. I can't seem to figure out the default path is to put at the last line... I'd like to to behave as normal in that regard. – Patm Sep 14 '11 at 22:34
6
How about this one:
The first resource I'd look at is http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in. Also, check out How to redirect to a specific page on successful sign up using rails devise gem? for some ideas.
You can do something like:
def after_sign_in_path_for(resource_or_scope)
session[:account_type] = current_user.account_type
end
You can implement this method in your ApplicationController or in a custom RegistrationsController.
Community
- 1
- 1
Mohit Jain
- 43,139
- 57
- 169
- 274