Set a session variable in development at login

I would like to set the session variable as soon as the user subscribes based on a specific field in the User table. I do not want to create a custom Devise controller if I do not need it. Is there any way? Or do I need to go the route of the user controller?

+6
source share
2 answers

There is a callback after_sign_in_path_for , you can add it to your ApplicationController

 protected def after_sign_in_path_for(resource) session[:domain_prefix] = current_user.domain_prefix user_path(resource) end 

Remember to return the path in the last line of the method, otherwise the callback will redirect the request to the contents of session[:domain_prefix]

+12
source

How about this:

The first resource I'm looking at is http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in . Also, see How to redirect to a specific page for a successful write 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 your custom RegistrationsController.

+5
source

Source: https://habr.com/ru/post/897348/


All Articles