3.5. Flash Messages

In my eyes, the term flash messages is somewhat misleading. Almost anyone would associate the term Flash with more or less colorful web pages that were implemented with the Adobe Shockwave Flash Plug-in. But in Rails, flash messages are something completely different. They are messages that are displayed, for example on the new page after a redirect (see Section 3.4, “Redirects”).
Flash messages are good friends with redirects. The two often work together in a team to give the user feedback on an action he just carried out. A typical example of a flash message is the system feedback when a user has logged in. Often the user is redirected back to the original page and gets the message You are now logged in.
As an example, we are once more constructing the ping pong scenario from Section 3.4, “Redirects”:
$ rails new pingpong

      [...]

$ cd pingpong 
$ rails generate controller Game ping pong

      [...]

$
We fill the app/controllers/game_controller.rb with the following content:
class GameController < ApplicationController
  def ping
   redirect_to game_pong_path, notice: 'Ping-Pong!'
  end

  def pong
  end
end
Now we start the Rails web server with rails server and use the browser to go to http://0.0.0.0:3000/game/ping. We are redirected from ping to pong. But the flash message "Ping-Pong!" is nowhere to be seen. We first need to expand app/views/layouts/application.html.erb:
<!DOCTYPE html>
<html>
<head>
  <title>Pingpong</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

<%- flash.each do |name, message| %>
<p><i><%= "#{name}: #{message}" %></i></p>
<% end %>

<%= yield %>

</body>
</html>
Now we see the flash message at the top of the page when we go to http://0.0.0.0:3000/game/ping in the browser:
Pong page with flash message
If we go to http://0.0.0.0:3000/game/pong we still see the normal Pong page. But if we go to http://0.0.0.0:3000/game/ping we are redirected to the Pong page and then the flash message is displayed at the top.

Tip

If you do not see a flash message that you were expecting, first check in the view to see if the flash message is output there.

Different Types of Flash Message

Flash messages are automagically passed to the view in a hash. By default, there are three different types: error, warning and notice. You can also invent your own category and then get it in the view later.

Different Syntaxes

Depending on the programmer's preferences, you will find different syntaxes of flash messages in practice. I am not going to take part in the discussion about which is the best syntax, but just show you briefly the two most common variations:
  • redirect_to game_pong_path, notice: 'Ping-Pong!'
  • flash[:notice] = 'Ping-Pong!'
    redirect_to action: pong

Why Are There Flash Messages At All?

You may wonder why there are flash messages in the first place. Couldn't you just build them yourself if you need them? Yes, indeed. But flash messages have the advantage that they offer a defined approach that is the same for any programmer. So you don't need to start from scratch every single time you need one.

Updates about this book will be published on my Twitter feed.