January 5th 2009
“Disabled option tags in rails forms”
Ever wanted to disable options in a form select in rails? I have; it feels more useable to me to disable out of stock product sizes:
The rails Form Option Helpers won’t let you do this as they currently stand. So until things change in core, I’ve put together the option_tags_will_disable plugin.
simply specify the disabled values when calling options_for_select:
1 2 3 4 5 6 |
options_for_select( ['Choose a size', 'small', 'medium', 'large'], nil, # selected value 'medium' # disabled value ) |
Gives you:
1 2 3 4 |
<option value="Choose a size">Please choose a size</option> <option value="s">small</option> <option value="m" disabled="disabled">medium</option> <option value="l">large</option> |
You can do the same when working with collections, but more interestingly, you can also give a Proc to identify which elements should be disabled:
1 2 3 4 5 |