I can'开发者_Go百科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..
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!
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.
to delete a user's session
session.delete(:user_id)
To clear only certain parameters, you can use:
[:param1, :param2, :param3].each { |k| session.delete(k) }
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
精彩评论