Working with the flash hash
AUG
17
2008
The flash hash is what Rails uses to display messages (both notices and errors). Since it's a Hash, you can assign any key/value pair that you want, but I tend to stick with flash[:notice] for a success message, and flash[:error] for an error message.
In my opinion, using the flash hash is a little confusing. I haven't come across a book or tutorial that fully explains how to control it properly. So, here are the options for using the flash hash in Rails.
flash[:key] and flash.now[:key]
The following method shows how to best use the flash hash:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to admin_users_path }
else
flash.now[:error] = 'The user could not be created'
format.html { render :action => 'new' }
end
end
rescue Exception => ex
logger.warn('ERROR: ' + ex.message)
flash.now[:error] = 'There was an error creating the user.'
render :action => 'new'
end
Notice that I use the flash hash in two different ways: flash[:key] and flash.now[:key]. The way I use it depends on when I want it displayed. The flash[:key] usage should only be used before redirection, because it makes the object available for the current action and the next action. The flash.now[:key] usage should be used when you only want the flash object to be available to the current action.
Here's an example why you shouldn't use flash[:key] without redirection. Let's say this is your controller:
class MainController < ApplicationController
def index
flash[:notice] = 'Welcome to the site!'
end
def profile
end
end
When you visit the index page, you'll see the message 'Welcome to the site!.' If you then click a link from the index page that takes you to the profile page, you'll still see the message 'Welcome to the site!' because the flash[:key] is available to the current action and the next action.
Displaying the flash
Here is a really helpful method to display the contents of the flash hash that I modified from one of Ryan Bates' awesome Railscasts:
<%- flash.each do |key, msg| -%>
<div id="<%= key %>">
<p style="float:right;"><%= link_to_function 'X', "Effect.Fade('#{key}')" %></p>
<p><%= msg %></p>
<div class="clear"></div>
</div>
<%- end -%>
This method will loop through each key in your flash hash and create a div with the name of the key, then put the contents inside with a link to close the message div.
I put this method in a partial called _notice_div.html.erb and include it at the top of my application layout. Here are the styles I use for notices and errors:
#notice { background-color: #A4E7A0; border: 1px solid #26722D; }
#error { background-color: #F0A8A8; border: 1px solid #900; }
#notice, #error { width: 90%; margin: 0 auto 10px auto; padding: 5px; }
#notice p, #error p { margin-left: 20px; padding: 0; font-size: .75em; color: #000; }
#notice a, #error a { text-decoration: none; padding: 0 3px; }
#notice a { border: 1px solid #26722D; color: #26722D; }
#error a { border: 1px solid #900; color: #900; }
#notice a:hover, #error a:hover { color: #333; border: 1px solid #333; }
This is a notice div.
This is an error div.
Tagged: tutorial, css, rails, flash
awesome! thanks dude!
posting from :new to :create, rendering :show, is this one action? does redirect_to always make a new action?
thaks for tips !
@Jesse: redirect_to always counts as a new action, render is always in the same action.
I have used this flash[:notice] for debugging the application variables, It shows the noticed temporary variable in "Show session dump" and it was working till rails 2.2.2 but from when I have shifted to rails 2.3.4, its not working at all, Is there any alternative of flash[:notice] to check the local variable check ??? Please do reply ASAP Thanks in Advance Naresh
@Naresh If you just need to check the value of your variables, you should really be using the debugger. Check out this link: http://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-ruby-debug
Leave a comment:
Popular Posts
Search
Tags
actionmailer activerecord ajax apache apple barcamp caching capistrano centos code golf css db delete eager loading ebay email attachment erb flash ftp fun generators get haml helpers ie sucks javascript jquery lightbox lost merb net ftp paperclip passenger php plexus post presentation rails rails machine railsconf redesign rest rjs routes rss ruby ruby on rails safari script sinatra symfony text replacement tips tutorial twitter xhtml
Projects
© 2010 Travis Roberts. All rights reserved.
Allan said on September 1, 2008:
flash.now[:key] usage was EXACTLY what I needed. Thanks for posting this!