This extension is part of the Sinatra::Contrib project. Run gem install sinatra-contrib to have it available.
Sinatra::Cookies¶ ↑
Easy way to deal with cookies
Usage¶ ↑
Allows you to read cookies:
get '/' do "value: #{cookies[:something]}" end
And of course to write cookies:
get '/set' do cookies[:something] = 'foobar' redirect to('/') end
And generally behaves like a hash:
get '/demo' do cookies.merge! 'foo' => 'bar', 'bar' => 'baz' cookies.keep_if { |key, value| key.start_with? 'b' } foo, bar = cookies.values_at 'foo', 'bar' "size: #{cookies.length}" end
Classic Application¶ ↑
In a classic application simply require the helpers, and start using them:
require "sinatra" require "sinatra/cookies" # The rest of your classic application code goes here...
Modular Application¶ ↑
In a modular application you need to require the helpers, and then tell the application to use them:
require "sinatra/base" require "sinatra/cookies" class MyApp < Sinatra::Base helpers Sinatra::Cookies # The rest of your modular application code goes here... end