On recent rails projects, I found myself clinging to a few useful helpers and additions. Here’s a few.
hide_unless
Often in views, I find I want to hide a particular div or element, but only if certain conditions are met. An ajax call might later reveal it, or replace the contents of the div with something.
1 2 3 |
|
In use:
1
|
|
present?
Rails gives us .blank?, but I hate writing things like:
1 2 3 |
|
So, I add this as an extension in my projects:
1 2 3 4 5 |
|
And obviously it works on anything: arrays, hashes, nil, etc.
1 2 3 |
|
UPDATE (29-Jun-2008): DHH just committed this to Edge Rails. I am certain that I had nothing to do with it, but I’ll pretend :)
user_owns_it
A common task is to check if the current user owns a particular resource.
1 2 3 |
|
This allows easy checking in views:
1 2 3 |
|
Please share if you have other interesting tidbits from your toolbox!