Tuesday, September 25, 2007

helper: collection_for_select

I didn't find the form helper that made collection select tags powerful enough, so I made my own. I don't know how to make a nice plugin yet, so it's still a helper.

Code is in pastie.

Put the "helper" section code in your helper file:

For usage, put the code in the "usage" section in your rhtml.

Notes:

* selected_value will only work with integers! It's possible this'll be changed in the future.

[EDIT]
2007-01-03: I changed the pastie code so an error won't be generated even if selected_value is nil

[EDIT]
2007-01-07: If the collection passed is empty, 2 things happen:
  1. The first option with value 0 is "Empty list"
  2. The list is disabled.
If you want to change the message when the list is empty, in the options hash, pass :empty_message

No comments: