Include blank for first item in select list in options_for_select tag

B Seven picture B Seven · Jul 15, 2011 · Viewed 78.8k times · Source

I tried :include_blank => true, but it didn't work.

<select>
    <%= options_for_select Model.all.collect{|mt| [mt.name, mt.id]} %>
</select>

If I need to add it to the collection, how would you do that?

Answer

Chris Barretto picture Chris Barretto · Jul 15, 2011

I think you want this format:

select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ] }, {:include_blank => 'name of your blank prompt'})

BTW: was assuming Modle was suppose to be Model. To use using collection_select:

collection_select(:model, :model_id, Model.all, :id, :name, :prompt => true)