Below is my select-form that works properly.
When the user loads the page it shall show an initial ‘select one...’ with value null or ‘’.
I tried to add it to the Object but wasn’t able to and would be glad to get help!
Thanks a lot!
In my view:
= select_tag 'incident[fault_id]' , options_from_collection_for_select( Fault.all, :id, :label)
I use Rails 3.2 and HAML
Update:
By chance I found something really sweet:
include_blank: 'select one...'
or completely
= f.collection_select :fault_id, Fault.order(:label), :id, :label, include_blank: 'select one...'
In case one likes that too...
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
options_from_collection_for_select returns a string of option tags that have been compiled by iterating over the collection and assigning the result of a call to the value_method as the option value and the text_method as the option text.
So just prepend it with "select_one" option string without value:
= select_tag 'incident[fault_id]', content_tag(:option,'select one...',:value=>"")+options_from_collection_for_select( Fault.all, :id, :label)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With