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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值