PrettyFlash

Rails plugin/gem that provides “pretty” css-driven flash messages and additional helpers to pull all errors from ActiveRecord objects and as well as errors on associated models of an ActiveRecord object.

In the controller…

This plugin/gem provides convenience methods for setting flash messages. Use this instead of the typical flash[:notice] = "Some String"


flash_success :message => "Hooray! You did something good."
flash_notice :message => "Hey! This is for tips and general information."
flash_warning :message => "Ummm, You're not allowed to do that yet."
flash_error :message => 'ID does not exist'

You can specify whether to use flash() or flash.now() via the options hash. The default is to use flash.now(). This is needed when setting flash and then redirecting. Also, any AJAX request defaults to flash.now()


flash_error :message => 'Blah', :now => false
redirect_to :list

You can also load validation errors from ActiveRecord Objects and any specified associated models


flash_error :object => @user, :associations => [:roles, :permissions]

The complete list of hash options for flash_success, flash_notice, flash_warning, flash_error is as follows: :message – the flash message to be added :now – Whether to use flash or flash.now :delimiter – separator between messages :clear_flash – delete flash for the type of flash called (:notice, :error, etc) :object – an ActiveRecord object with @errors :associations – model associations of the AR Object that should be checked for errors

In the view…

Then all you have to do to display them is:

<%= display_flash_messages %>

Easy enough?

There are also methods to check if any error, notice, warning or success messages exist.


flash_has_successes?
flash_has_notices?
flash_has_warnings?
flash_has_errors?

Get Started

Install the Gem

sudo gem install gbdev-pretty_flash

Add the gem to your environment.rb file

config.gem 'gbdev-pretty_flash', :lib => 'pretty_flash', :version => '0.1.0', :source => 'http://gems.github.com'

There’s a public folder with this plugin/gem that contains the images, CSS, and javascript (optional). The first time you run your application with the added config.gem line, the images, stylesheet, and javascript will be copied to your public folder.

On subsequent application starts, the plugin/gem checks for the presence of public/images/flash-error.png. If this file is present, nothing will be copied to your public directory.

WARNING: this will overwrite any of the files that have the same name. So double check if you don’t want that to happen.

Also, the pretty_flash.js file located in the public/javascripts folder is only used to fade the flash messages after so many seconds (defaults to 2.5 seconds). If you want that capability, just copy the relevant section of code (prototype or jquery) to application.js and you’re all set.

Credits

Original plugin by Ryan Heath (http://github.com/rpheath/pretty_flash). Converted to a gem and extensively modified by Great Basin Solutions, LLC.

Licensing

Copyright © 2008 Ryan Heath, released under the MIT license