This example Sinatra application implements a common feature in web applications: displaying a temporary message about the results of a previous HTTP request.
A "flash" message is a temporary message that is passed from one request (A) to another (B), but disappears after the subsequent request (B) has been handled.
For example, if you have these routes:
get '/' do
get_flash(:my_message)
end
post '/message' do
set_flash(:my_message, 'Hello!')
redirect '/'
endYou could send a POST request to /message, be redirected to /, and you receive a response with body 'Hello!'. A subsequent GET request to / would not return any value for get_flash(:my_message) though.
Flash messages are useful for addressing a variety of situations where you need a message passed between requests. For example:
- Notifying a user of errors when attempting to persist a resource
- Showing a welcome message after logging in
- Displaying a confirmation message when a user action is successful
Look at the code in app-flash-messages.rb to see the custom "flash" behavior built on top of Sinatra's session feature. This feature is inspired by the rack-flash and Rails' ActionDispatch::Flash.
Make sure you're in the flash-messages directory. Once there, run the following two commands to configure your application correctly:
$ bundle install --without production
$ rake setup:dotenv
If you're using Cloud9, run the application with
$ rerun bundle exec rackup -p $PORT -o $IP
If you're on your own computer, run the application with
$ rerun bundle exec rackup
If everything runs successfully, you should be able to visit http://localhost:9292 and see the web application.