Sessions & Cookies

Sessions keep state across requests. They can be enabled by using any Rack session middleware (defined in app.rb):

middleware do |builder|
  builder.use Rack:Session::Cookie

Once configured, sessions can be set and fetched through the sessions helper:

session[:foo] = 'bar'

puts session[:foo]
# => bar

Cookies can be set and fetched the same way:

cookies[:foo] = 'bar'

puts cookies[:foo]
# => bar

By default, a cookie is created for path '/' and is set to expire in seven days. These defaults can be overridden by using the Response#set_cookie method:

response.set_cookie :foo,
  value: 'bar',
  expires: + 3600,
  path: '/login'