3
# Access the contents of the flash. Use <tt>flash["notice"]</tt> to
4
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
7
@env[Flash::KEY] ||= (session["flash"] || Flash::FlashHash.new)
11
# The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed
12
# to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create
13
# action that sets <tt>flash[:notice] = "Post successfully created"</tt> before redirecting to a display action that can
14
# then expose the flash to its template. Actually, that exposure is automatically done. Example:
16
# class PostsController < ActionController::Base
19
# flash[:notice] = "Post successfully created"
20
# redirect_to posts_path(@post)
24
# # doesn't need to assign the flash notice to the template, that's done automatically
29
# <% if flash[:notice] %>
30
# <div class="notice"><%= flash[:notice] %></div>
33
# Since the +notice+ and +alert+ keys are a common idiom, convenience accessors are available:
35
# flash.alert = "You must be logged in"
36
# flash.notice = "Post successfully created"
38
# This example just places a string in the flash, but you can put any object in there. And of course, you can put as
39
# many as you like at a time too. Just remember: They'll be gone by the time the next action has been performed.
41
# See docs on the FlashHash class for more details about the flash.
43
KEY = 'action_dispatch.request.flash_hash'.freeze
45
class FlashNow #:nodoc:
62
# Convenience accessor for flash.now[:alert]=
64
self[:alert] = message
67
# Convenience accessor for flash.now[:notice]=
69
self[:notice] = message
73
# Implementation detail: please do not change the signature of the
74
# FlashHash class. Doing that will likely affect all Rails apps in
75
# production as the FlashHash currently stored in their sessions will
80
def initialize #:nodoc:
87
def initialize_copy(other)
88
if other.now_is_loaded?
95
def []=(k, v) #:nodoc:
104
def update(h) #:nodoc:
105
h.keys.each { |k| keep(k) }
136
@flashes.each(&block)
139
alias :merge! :update
141
def replace(h) #:nodoc:
147
# Sets a flash that will not be available to the next action, only to the current.
149
# flash.now[:message] = "Hello current action"
151
# This method enables you to use the flash as a central messaging system in your app.
152
# When you need to pass an object to the next action, you use the standard flash assign (<tt>[]=</tt>).
153
# When you need to pass an object to the current action, you use <tt>now</tt>, and your object will
154
# vanish when the current action is done.
156
# Entries set via <tt>now</tt> are accessed the same way as standard entries: <tt>flash['my-key']</tt>.
158
@now ||= FlashNow.new(self)
161
# Keeps either the entire current flash or a specific flash entry available for the next action:
163
# flash.keep # keeps the entire flash
164
# flash.keep(:notice) # keeps only the "notice" entry, the rest of the flash is discarded
169
# Marks the entire flash or a single flash entry to be discarded by the end of the current action:
171
# flash.discard # discard the entire flash at the end of the current action
172
# flash.discard(:warning) # discard only the "warning" entry at the end of the current action
177
# Mark for removal entries that were kept, and delete unkept ones.
179
# This method is called automatically by filters, so you generally don't need to care about it.
182
unless @used.include?(k)
190
# clean up after keys that could have been left over by calling reject! or shift on the flash
191
(@used - keys).each{ |k| @used.delete(k) }
194
# Convenience accessor for flash[:alert]
199
# Convenience accessor for flash[:alert]=
201
self[:alert] = message
204
# Convenience accessor for flash[:notice]
209
# Convenience accessor for flash[:notice]=
211
self[:notice] = message
220
# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
221
# use() # marks the entire flash as used
222
# use('msg') # marks the "msg" entry as used
223
# use(nil, false) # marks the entire flash as unused (keeps it around for one more action)
224
# use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action)
225
# Returns the single value for the key you asked to be marked (un)used or the FlashHash itself
226
# if no key is passed.
227
def use(key = nil, used = true)
228
Array(key || keys).each { |k| used ? @used << k : @used.delete(k) }
229
return key ? self[key] : self
238
if (session = env['rack.session']) && (flash = session['flash'])
244
session = env['rack.session'] || {}
245
flash_hash = env[KEY]
248
if !flash_hash.empty? || session.key?('flash')
249
session["flash"] = flash_hash
250
new_hash = flash_hash.dup
252
new_hash = flash_hash
258
if session.key?('flash') && session['flash'].empty?
259
session.delete('flash')