How to empty/destroy a session in rails?

后端 未结 5 2061
执念已碎
执念已碎 2020-12-04 10:40

I can\'t seem to find it anywhere... How do I delete/destroy/reset/empty/clear a user\'s session in Rails? Not just one value but the whole thing..

相关标签:
5条回答
  • 2020-12-04 10:56

    To clear only certain parameters, you can use:

    [:param1, :param2, :param3].each { |k| session.delete(k) }
    
    0 讨论(0)
  • 2020-12-04 11:00

    add this code to your ApplicationController

    def reset_session
      @_request.reset_session
    end
    

    ( Dont know why no one above just mention this code as it fixed my problem ) http://apidock.com/rails/ActionController/RackDelegation/reset_session

    0 讨论(0)
  • 2020-12-04 11:15

    To clear the whole thing use the reset_session method in a controller.

    reset_session
    

    Here's the documentation on this method: http://api.rubyonrails.org/classes/ActionController/Base.html#M000668

    Resets the session by clearing out all the objects stored within and initializing a new session object.

    Good luck!

    0 讨论(0)
  • 2020-12-04 11:22

    session in rails is a hash object. Hence any function available for clearing hash will work with sessions.

    session.clear
    

    or if specific keys have to be destroyed:

    session.delete(key)
    

    Tested in rails 3.2

    added

    People have mentioned by session={} is a bad idea. Regarding session.clear, Lobati comments- It looks like you're probably better off using reset_session [than session.clear], as it does some other cleaning up beyond what session.clear does. Internally, reset_session calls session.destroy, which itself calls clear as well some other stuff.

    0 讨论(0)
  • 2020-12-04 11:23

    to delete a user's session

    session.delete(:user_id)
    
    0 讨论(0)
提交回复
热议问题