Sinatra::Flash

Sinatra::Flash is an extension that lets you store information between requests.

Often, when an application processes a request, it will redirect to another URL upon finishing, which generates another request.

This means that any information from the previous request is lost (due to the stateless nature of HTTP).

Sinatra::Flash overcomes this by providing access to the flash—a hash-like object that stores temporary values such as error messages so that they can be retrieved later—usually on the next request.

It also removes the information once it’s been used.

All this can be achieved via sessions (and that’s exactly how Sinatra::Flash does it), but Sinatra::Flash is easy to implement and provides a number of helper methods.

Ejemplo

[~/sinatra/sinatra-flash]$ cat app.rb 
require 'sinatra'
require 'sinatra/flash'

enable :sessions

get '/blah' do
  # This message won't be seen until the NEXT Web request that accesses the flash collection
  flash[:blah] = "You were feeling blah at #{Time.now}."

  # Accessing the flash displays messages set from the LAST request
  "Feeling blah again? That's too bad. #{flash[:blah]}"
end

get '/pum' do
  # This message won't be seen until the NEXT Web request that accesses the flash collection
  flash[:pum] = "You were feeling pum at #{Time.now}."

  # Accessing the flash displays messages set from the LAST request
  "Feeling pum again? That's too bad. #{flash[:pum]}"
end

Gemfile

[~/sinatra/sinatra-flash]$ cat Gemfile
source 'https://rubygems.org'

gem 'sinatra'
gem 'sinatra-flash'



Subsecciones
Casiano Rodríguez León
2015-01-25