rubycookiesrubygemsrack

no method error when trying to initialize rack session cookies


I'm using no frameworks, only the rack gem to build a web application. I'm trying to set cookies to an successfully logged user:

use Rack::Session::Cookie

I'm getting the following error:

C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rack-2.2.7/lib/rack/session/cookie.rb:155:in `<class:Cookie>': undefined method `DelegateClass' for Rack::Session::Cookie:Class (NoMethodError)

I already tried to specify rack version as

gem "rack", "~> 3.0"

but nothing changed.

How to fix it?


Solution

  • You probably recently updated from rack version 2 to 3. Or you are following a tutorial or documentation for rack version 2 but while running rack 3.

    Quote from rack's UPGRADE-GUIDE.md:

    Rack::Session was moved to a separate gem.

    Previously, Rack::Session was part of the rack gem. Not every application needs it, and it increases the security surface area of the rack, so it was decided to extract it into its own gem rack-session which can be updated independently.

    Applications that make use of rack-session will need to add that gem as a dependency:

    gem 'rack-session'