Skip to main content

Control Where Flash Messages Are Rendered

Flash Messages

Like many other features of this gem, your application's flash messages are displayed in a default location on the page, but you have the ability to override this within your page templates.

Ordinarily, these messages appear below the header bar and page headline:

# pmacs-frontend/_content.html.erb
<% unless content_for?(:flash_alerts) %>
  <%= get_flash_alerts %>
<% end %>

Note the check for existing content_for in this partial. The get_flash_alerts helper method defines this property when it executes, which means in practice that it will only ever render once in any given render cycle. Rails' rendering runs "inside-out": partials are rendered before templates which are rendered before layouts. Further, local views are rendered before gem-provided views. This gives you a lot of flexibility if you want to override the default display of flash messages.

If you'd like to display your flash messages somewhere specific in your template, simply add:

<%= get_flash_alerts %>

Wherever that appears, your flash messages (if any) will be rendered. No other instances will appear anywhere.

Banners

Another option for flash messages is to render them in the banners area, directly below the header and above the navigation and content columns. To bring your flash messages into the banner, you need to add the key type: :banner into the flash setter method.

flash[:notice] = {
  type: :banner,
  message: 'Some message here'
}