/
sinatra-cookies.html
58 lines (55 loc) · 1.21 KB
/
sinatra-cookies.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<h1>Sinatra::Cookies</h1>
<p>
Easy way to deal with cookies
</p>
<h2>Usage</h2>
<p>
Allows you to read cookies:
</p>
<pre>
get '/' do
"value: #{cookie[:something]}"
end
</pre>
<p>
And of course to write cookies:
</p>
<pre>
get '/set' do
cookies[:something] = 'foobar'
redirect to('/')
end
</pre>
<p>
And generally behaves like a hash:
</p>
<pre>
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
</pre>
<h3>Classic Application</h3>
<p>
In a classic application simply require the helpers, and start using them:
</p>
<pre>
require "sinatra"
require "sinatra/cookies"
# 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 "sinatra/base"
require "sinatra/link_header"
class MyApp < Sinatra::Base
helpers Sinatra::Cookies
# The rest of your modular application code goes here...
end
</pre>