How to empty/destroy a session in rails?

Ruby on-RailsSessionSession VariablesSession Timeout

Ruby on-Rails Problem Overview


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..

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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!

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

to delete a user's session

session.delete(:user_id)

Solution 4 - Ruby on-Rails

To clear only certain parameters, you can use:

[:param1, :param2, :param3].each { |k| session.delete(k) }

Solution 5 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontybro0103View Question on Stackoverflow
Solution 1 - Ruby on-RailsGdeglinView Answer on Stackoverflow
Solution 2 - Ruby on-RailsLavixuView Answer on Stackoverflow
Solution 3 - Ruby on-Railsvjnan369View Answer on Stackoverflow
Solution 4 - Ruby on-RailsvladCovaliovView Answer on Stackoverflow
Solution 5 - Ruby on-RailsDavid MesarosView Answer on Stackoverflow