R – How long do objects stored in Rails’ sessions persist

ruby-on-railssession

If I do session[:greeting] = "Hi!" in one controller action, for how long will I be able to refer to session[:greeting] in other controller actions?

Until the user closes his browser?

Until a certain amount of time passes?

Also, how can I configure this value?

Best Answer

Until the user closes her browser. That's the definition of a session.

To configure something longer, you will need to use one of:

  • cookies. These can be marked to stay for any period of time (or until the user closes the browser)
  • have the user log in

Often there's a combination of these, where the user is given a "remember me" token as a cookie, so that they don't have to log in every time they restart the browser.