module

The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create action that sets flash[:notice] = "Successfully created" before redirecting to a display action that can then expose the flash to its template. Actually, that exposure is automatically done. Example:

  class WeblogController < ActionController::Base
    def create
      # save post
      flash[:notice] = "Successfully created post"
      redirect_to :action => "display", :params => { :id => post.id }
    end

    def display
      # doesn't need to assign the flash notice to the template, that's done automatically
    end
  end

  display.erb
    <% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>

This example just places a string in the flash, but you can put any object in there. And of course, you can put as many as you like at a time too. Just remember: They'll be gone by the time the next action has been performed.

See docs on the FlashHash class for more details about the flash.

Included modules

  • InstanceMethods

Files

  • actionpack/lib/action_controller/flash.rb

Nested classes and modules

4Notes

Application Helper for Fading Flash Messages

ncancelliere · Nov 16, 200811 thanks

A simple helper method for showing the flash message. Includes optional fade in seconds (view needs javascript_include_tag defaults if you desire fade effect):

def show_flash_message(options={})
html = content_tag(:div, flash.collect{ |key,msg| content_tag(:div, msg, :class => key) }, :id => 'flash-message')
if options.key?(:fade)
  html << content_tag(:script, "setTimeout(\\"new Effect.Fade('flash-message');\\",#{options[:fade]*1000})", :type => 'text/javascript')
end
html
end

simply call in your views then using:

<%= show_flash_message(:fade => 4) %>

Keeping the flash object on multiple redirects

tokland · Jun 11, 20094 thanks

If your controllers are redirecting more than once, the flash contents will be lost. To avoid it, execute flash.keep before each redirection.

Check ActionController::Flash::FlashHash for more handy methods (discard, now, ...)

Broadened Flash helper

insane-dreamer · Mar 20, 20092 thanks

Building on the below excellent example, you can create something with default options for how long it's displayed and how long the fade is, and highlight:

def show_flash(options={})
options = {:fade => 3, :display => 3, :highlight => true}.merge(options)
html = content_tag(:div, flash.collect{ |key,msg| content_tag(:div, msg, :class => key, :attributes => "style = display: none;") }, :id => 'flash-message')
html << content_tag(:script, "new Effect.Highlight('flash-message');") if options[:highlight]
html << content_tag(:script, "$('flash-message').appear();")
html << content_tag(:script, "setTimeout(\\"$('flash-message').fade({duration: #{options[:fade]}});\\", #{options[:display]*1000});")
end

Handling flash for ajax calls

Milind · Aug 5, 2014

This is helper method which can be easily used to handle flash for ajax calls ##call it in your js.erb def flash_display response = "" flash.each do |name, msg| msg=msg+"".html_safe response = response + content_tag(:div, msg, :id => "flash_# {name}",:class=>"alert alert-danger") do "#{msg}".html_safe
end end flash.discard response end

so in your controller,

def get_data_and_update_div

@user_details=User.get_details if @user_details.nil? flash.now[:error]="Add your details and then submit" format.js { render 'shared/error_messages'} end

in error_messages.js.erb

$('#flash_messages').html("<%= escape_javascript raw(flash_display) %>");

in view file

<div id="flash_messages">
<% if flash[:error] %>
 <%= content_tag :div, flash['value'], :id =>"flash_error" %>

<% end %>