Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (33 sloc) 1.14 KB
<p>Easy way to deal with cookies</p>
<p>Allows you to read cookies:</p>
<pre>get '/' do
&quot;value: #{cookies[:something]}&quot;
<p>And of course to write cookies:</p>
<pre>get '/set' do
cookies[:something] = 'foobar'
redirect to('/')
<p>And generally behaves like a hash:</p>
<pre>get '/demo' do
cookies.merge! 'foo' =&gt; 'bar', 'bar' =&gt; 'baz'
cookies.keep_if { |key, value| key.start_with? 'b' }
foo, bar = cookies.values_at 'foo', 'bar'
&quot;size: #{cookies.length}&quot;
<h3>Classic Application</h3>
<p>In a classic application simply require the helpers, and start using them:</p>
<pre>require &quot;sinatra&quot;
require &quot;sinatra/cookies&quot;
# The rest of your classic application code goes here...</pre>
<h3>Modular Application</h3>
<p>In a modular application you need to require the helpers, and then tell the
application you will use them:</p>
<pre>require &quot;sinatra/base&quot;
require &quot;sinatra/cookies&quot;
class MyApp &lt; Sinatra::Base
helpers Sinatra::Cookies
# The rest of your modular application code goes here...
Jump to Line
Something went wrong with that request. Please try again.