`

Rails plugin for pretty flash messages

阅读更多

Rails plugin for pretty flash messages

http://rpheath.com/posts/379-rails-plugin-for-pretty-flash-messages

Displaying flash messages is, for the most part, pretty consistent across all of my applications. I usually make use of three: notice, warning, and error. The notices are typically some shade of green, warnings are usually yellow-ish, and errors are always some form of red.

It’s tedious to keep doing the same “flash” stuff in every application. So I wrote a plugin that will hopefully take that pain away. It’s called pretty_flash, and can be found on GitHub. It’s really just for me, but generic enough to share.

Once setup, by default the three flash messages will look like:

Not too bad, but feel free to change it if you aren’t into pastels :-)

I’m not fond of setting flash messages via:

1
flash[:whatever] = "This is the whatever message."

So I’ve added a couple of convenience methods to replace that. Now, in your controller you can set flash messages like this:

1
2
3
notice "This is a notice"
warning "This is a warning"
error "This is an error"

And to display them to the world, somewhere in your view/layout, just call:

1
<%= display_flash_messages %>

It will loop through all the messages set and display each one.

Since this plugin involves CSS and a few images, I’ve provided a rake task that will put those things in their proper places. You can simply copy what you need if you’re not lazy.

1
$ rake pretty_flash:install

But!

You may want to check the assets/ folder in the plugin directory first, just to make sure you don’t have any files with the same name (the rake task will overwrite them if so).

Oh, and another thing. I always fade my flash messages after so long. I typically use either jQuery or Prototype to handle this, so I’ve added a flash.js file that covers both. If you so desire, just copy whichever snippet into your application.js file, and your flash messages will start fading after so many seconds.

Again, this plugin is really just for me, but I figured someone else might find it useful.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics