20
use Rack::Session::Pool
...
session[:msg]="Hello Rack"

EDIT: The word session doesn't seem to resolve. I included the Session pool middleware in my config.ru, and try to set a variable in an ERB file (I'm using Ruby Serve) and it complains "undefined local variable or method `session'"

Thanks!

Sunder
  • 1,445
  • 2
  • 12
  • 22
  • 2
    what doesn't work exactly? is the session helper not working at all, or just is it that session[:msg] isn't set? what does session.inspect give you? – oliverbarnes May 04 '12 at 15:23

2 Answers2

33

session is a method that is part of some web frameworks, for example Sinatra and Rails both have session methods. Plain rack applications don’t have a session method, unless you add one yourself.

The session hash is stored in the rack env hash under the key rack.session, so you can access it like this (assuming you’ve named the rack environment to your app env):

env['rack.session'][:msg]="Hello Rack"

Alternatively, you could use Rack’s built in request object, like this:

request = Rack::Request.new(env)
request.session[:msg]="Hello Rack"
matt
  • 78,533
  • 8
  • 163
  • 197
  • But then why you can also do: `request.env['rack.session'][:msg]="Hello Rack"` ? If I understand this correctly, `Rack::Request` is a wrapper around `env` but if so, then how does `request.env[...` make sense? I can't get my head around it :\ – Redoman Oct 07 '15 at 04:40
  • @jj_`Rack::Request` is a wrapper around the `env` hash that allows you to do various common things easily, and it also provides access to the `env` hash n case you need to use it directly for something that `Rack::Request` doesn’t provide. – matt Oct 07 '15 at 13:11
  • Thanks for confirming that and explaining... however, isn't the fact that `#env` is available on `Rack::Request` a bit confusing? `Rack::Request `encapsulates `env`, but at the same time `#env` is available to `Rack::Request` to provide `env` in its raw form.. **why so when the original `env` is still available everywhere, already?** Maybe from a class designer point of view is just to make `Rack::Request` more feature-complete on its own? Just trying to understand the rationale behind it... Thanks! – Redoman Oct 07 '15 at 14:32
  • @jj_ Yes you could just use `env` directly and not bother with `Rack::Response`, but the class provides methods like `scheme` and `host` which you would need to look up various headers and how they are used in order to replicate, as well as easy access to the request parameters (including parsing query strings), so it’s just more convenient to use. – matt Oct 07 '15 at 15:28
  • I don't find this works with `rack 1.6` and rails `4.2`. I cannot find an interface that works. – New Alexandria Dec 20 '15 at 02:43
  • I had to do this: ```env['rack.session.options'][:skip] = true``` to get it to save – user1881102 Jan 18 '18 at 22:45
0

You need to load rack::session module next probably cookie like here http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html

This like explains it with example.

Jakub Oboza
  • 5,291
  • 1
  • 19
  • 10