简体   繁体   中英

How to add or remove a record in database based on user login/logout, I am using Devise gem for Ruby on Rails?

I have certain user settings which I need to load when user logs in & destroy his custom settings after he logs out. I am using Devise gem. Also I intend to keep this settings record id in Rails session for easy access through out the application.

when the user logs out, I want to do something like this:

Setting.delete(session[:settings_id])

where Setting is my Model class, and I am holding the currently loaded settings_id in session as session[:settings_id] = Settings.where(:conditions => hash).id

then when the user logs out, I will have to delete the record from my settings table using

    Setting.delete(session[:settings_id]),

since I already have currently loaded settings_id kept in session. How can I do it alongside Devise ?

Devise is build upon warden which provides some callbacks you may use:

Warden::Manager.before_logout do |user, auth, opts|
  Setting.delete(auth.session[:settings_id])
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM