Default option in collection_select


#1

I need a way to set a default in a collection_select. Any way?


#2

I use:

options_for_select(container, selected = nil)

Hope it helps…


#3

You can create a helper like this one using options_for_select

def my_select(object, method, choices, options = {})
html = “”
if options[:id]
html << “”
else
html << “”
end
html << options_for_select(choices, options[:selected])
html << “”
html
end

And call it from the view
<%= my_select ‘telephone’, ‘type’, @telephone_types, :selected =>
‘Mobile’ %>

Or if you want a blank option at the top (or display any text) you can
use the :prompt option.

<%= select(‘telephone’, ‘type’, @telephone_types, {:prompt => “Select
Type”}) %>

Hope this helps

Rafa