Search code examples
sessionruby-on-rails-2

How do you clear a single entry from a ruby on rails session?


In ruby on rails when doing session[:foo] = nil it leaves an entry named :foo in the session object. How can you get rid of that single entry from the session object?


Solution

  • Actually there is a way to get delete a value from the session. Like RichH said the session variable is a CGI::Session instance. When enter something like session[:foo] it is actually looking that symbol up in a @data instance variable in the session object. That data variable is a hash.

    EDIT: There is a data instance variable in the CGI::Session class. If you go to the docs and look at the source code for the []= method you'll see that there is a @data member.

    So to delete session[:foo] all you have to do is access that @data variable from inside the session

       session.data[:foo]
    

    Now to delete it:

       session.data.delete :foo
    

    Once you do this the there should be no more foo in your session variable.