Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 'select one...' to options_from_collection_for_select

Tags:

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

like image 897
BBQ Chef Avatar asked Jul 27 '12 08:07

BBQ Chef


1 Answers

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) 
like image 77
rogal111 Avatar answered Sep 20 '22 15:09

rogal111