Sessions

Database-backed sessions

Do the following if you want to have sessions that are stored in the database.

  1. Open config/environment.rb
  2. Uncomment the line config.action_controller.session_store = :active_record_store
  3. On the command line, run rake db:sessions:create. This will create a migrations file, ###_add_sessions.rb
  4. Run rake db:migrate.

Cookie-based sessions

Rails 2.0 has better support for cookie-based sessions and supports it out of the box.

To help configure sessions, do the following.

  1. Add to your /config/environment.rb
      require 'yaml'
      session = YAML.load_file('config/session.yml')
      config.action_controller.session = {
        :session_key => session[RAILS_ENV]['session_key'],
        :secret      => session[RAILS_ENV]['secret']
      }
    

  2. Add a file called session.yml in the directory /config and edit it with your own values.
    # This is an example settings file for Rails 2.0 cookie-based sessions.
    # To generate a secret phrase, use 'rake secret'
    development:
      session_key: _session_key
      secret: # a hashed string that is longer than 32 characters
    
    test:
      session_key: _session_key
      secret: # a hashed string that is longer than 32 characters
    
    production:
      session_key: _session_key
      secret: # a hashed string that is longer than 32 characters
    

  3. As noted in the above file, generate a secret phrase by invoking rake secret on the command line.