Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 98 lines (64 sloc) 2.547 kB
dea5205 @nakajima IMPORTANT: Sinatra::Flash is now Rack::Flash.
authored
1 # Rack Flash
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
2
e265031 @nakajima Removed metaid dependency and bumped version
authored
3 flash[:notice] = "You can stop rolling your own now."
4
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
5 Simple flash hash implementation for Rack apps.
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
6
0c80686 @nakajima Added link to RDoc
authored
7 [View the RDoc](http://gitrdoc.com/nakajima/rack-flash/tree/master).
8
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
9 ## Usage
10
5c23efe @nakajima Added :accessorize option to dynamically generate accessors.
authored
11 Here's how to use it.
12
8729130 @treeder Added rack-flash3 install instructions.
treeder authored
13 ### Install / add to Gemfile
14
15 gem 'rack-flash3'
16
5c23efe @nakajima Added :accessorize option to dynamically generate accessors.
authored
17 ### Vanilla Rack apps
18
fa8e736 @nakajima Switch to more spec friendly env key.
authored
19 You can access flash entries via `env['x-rack.flash']`. You can treat it either
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
20 like a regular flash hash:
21
fa8e736 @nakajima Switch to more spec friendly env key.
authored
22 env['x-rack.flash'][:notice] = 'You have logged out.'
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
23
24 Or you can pass the `:accessorize` option to declare your flash types. Each of
25 these will have accessors defined on the flash object:
26
27 use Rack::Flash, :accessorize => [:notice, :error]
28
29 # Set a flash entry
fa8e736 @nakajima Switch to more spec friendly env key.
authored
30 env['x-rack.flash'].notice = 'You have logged out.'
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
31
32 # Get a flash entry
fa8e736 @nakajima Switch to more spec friendly env key.
authored
33 env['x-rack.flash'].notice # => 'You have logged out.'
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
34
35 # Set a a flash entry for only the current request
fa8e736 @nakajima Switch to more spec friendly env key.
authored
36 env['x-rack.flash'].notice! 'You have logged out.'
5c23efe @nakajima Added :accessorize option to dynamically generate accessors.
authored
37
38 Sample rack app:
39
40 get = proc { |env|
41 [200, {},
fa8e736 @nakajima Switch to more spec friendly env key.
authored
42 env['x-rack.flash'].notice || 'No flash set. Try going to /set'
5c23efe @nakajima Added :accessorize option to dynamically generate accessors.
authored
43 ]
44 }
45
46 set = proc { |env|
fa8e736 @nakajima Switch to more spec friendly env key.
authored
47 env['x-rack.flash'].notice = 'Hey, the flash was set!'
5c23efe @nakajima Added :accessorize option to dynamically generate accessors.
authored
48 [302, {'Location' => '/'},
49 'You are being redirected.'
50 ]
51 }
52
53 builder = Rack::Builder.new do
54 use Rack::Session::Cookie
55 use Rack::Flash, :accessorize => true
56
57 map('/set') { run set }
58 map('/') { run get }
59 end
60
61 Rack::Handler::Mongrel.run builder, :Port => 9292
62
63 ### Sinatra
64
65 If you're using Sinatra, you can use the flash hash just like in Rails:
dea5205 @nakajima IMPORTANT: Sinatra::Flash is now Rack::Flash.
authored
66
6293a16 @nakajima Abide by middleware dir convention
authored
67 require 'sinatra/base'
68 require 'rack-flash'
69
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
70 class MyApp < Sinatra::Base
9027f14 @akahn Make Sinatra example in README enable sessions.
akahn authored
71 enable :sessions
dea5205 @nakajima IMPORTANT: Sinatra::Flash is now Rack::Flash.
authored
72 use Rack::Flash
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
73
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
74 post '/set-flash' do
75 # Set a flash entry
76 flash[:notice] = "Thanks for signing up!"
77
78 # Get a flash entry
79 flash[:notice] # => "Thanks for signing up!"
80
81 # Set a flash entry for only the current request
82 flash.now[:notice] = "Thanks for signing up!"
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
83 end
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
84 end
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
85
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
86 If you've got any ideas on how to simplify access to the flash hash for vanilla
87 Rack apps, let me know. It still feels a bit off to me.
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
88
c7f7519 @nakajima Cleaned up dynamic accessors (thanks foca), added flash.now
authored
89 ## Sweeping stale entries
90
91 By default Rack::Flash has slightly different behavior than Rails in that it
92 doesn't delete entries until they are used. If you want entries to be cleared
93 even if they are not ever accessed, you can use the `:sweep` option:
94
95 use Rack::Flash, :sweep => true
21f51c5 @nakajima Implemented simple flash hash for Sinatra.
authored
96
9027f14 @akahn Make Sinatra example in README enable sessions.
akahn authored
97 This will sweep stale flash entries, whether or not you actually use them.
Something went wrong with that request. Please try again.