63

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?

Arslan Ali
  • 16,294
  • 7
  • 51
  • 65
B Seven
  • 39,557
  • 59
  • 208
  • 346
  • 1
    I believe it is the same in rails 2 and 3? You probably want to use the collection_select helper instead of html select tag - more info on include_blank - http://house9.blogspot.com/2009/04/rails-collectionselect-to-prompt-or.html – house9 Jul 15 '11 at 17:40

6 Answers6

93

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)
Chris Barretto
  • 8,761
  • 3
  • 39
  • 45
  • 1
    It works also for a lot of other Rails helpers. I just tried `grouped_collection_select` and it works. Thanks @chris – htaidirt Jul 08 '14 at 15:34
  • Anytime @htaidirt. Glad to help – Chris Barretto Jul 13 '14 at 06:37
  • 3
    FYI, if you're using `select_tag` and you want to include some text for the blank option, you need to use `prompt`, as `include_blank` is only true/false. – Joshua Pinter Jun 15 '15 at 21:01
  • @JoshPinter include_blank can also be set to a string per http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html ":include_blank - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element." This works in Rails 4.2 and possibly earlier. – Maltiriel Sep 20 '16 at 14:53
  • @Maltiriel Using `select_tag` on Rails 3 doesn't show the string if you pass it one. I was expecting it to, like the other select methods, but it doesn't. That's why I left a comment to help others. You can see the source here simply delete the `include_blank` value: http://apidock.com/rails/v3.2.13/ActionView/Helpers/FormTagHelper/select_tag – Joshua Pinter Sep 20 '16 at 15:30
  • It's not working on edit. It gives me the same value not the blank one! – nourza Dec 27 '20 at 01:36
27

I believe the :include_blank options only exist for select fields tied to a model.

Assuming you want to use a plain <select> tag instead of a <%= select(...) %> tied to a model, you can insert a blank entry at the front of your results:

<%= options_for_select Modle.all.collect{|mt| [mt.name, mt.id]}.insert(0, "") %>
Dylan Markow
  • 117,383
  • 23
  • 273
  • 197
  • 8
    This is false. `include_blank:` is an option on the select_tag method, so it will work regardless. – tubbo Sep 21 '15 at 17:04
  • @tubbo `include_blank:` is great and it is best rails way option. But when only updating collection parameter, then this tricky code is very useful. – Penguin Mar 10 '17 at 09:33
22

Since you have tagged as select-tag you can use the option include_blank with select_tag.

From the documentation:

select_tag "people", options_from_collection_for_select(@people, "id", "name"), :include_blank => true

# => <select id="people" name="people"><option value=""></option><option value="1">David</option></select>

Or you can use options_for_select:

<%= select_tag column.name, options_for_select(Model.all.collect{|mt| [mt.name, mt.id]}), :include_blank => true %>
Paulo Fidalgo
  • 19,844
  • 7
  • 85
  • 108
  • 5
    This is the actual answer to the question. The others involve changing the input structure. – thatmiddleway Jul 17 '14 at 18:55
  • 2
    FYI, with a `select_tag` (notice the `_tag` part) `include_blank` will not display a string that you pass to it. You will need to use `prompt` instead of that. – Joshua Pinter Jun 15 '15 at 21:00
  • 1
    This shows a blank option but there is a problem: when editing an existing entity with a specified value for a field, the stored value in the DB is not loaded but what is shown is invariably a blank option. – W.M. Dec 13 '16 at 13:44
8
<%= options_for_select Model.all.collect{|x| [x.name,x.id]}.unshift(["",nil]) %>
Weston Ganger
  • 4,956
  • 2
  • 35
  • 37
1
= select_tag "some_value", options_for_select(Model.all.collect{ |x| [x.name, x.id]}.prepend([t('helpers.some_name'), nil]), :selected => params[:some_value])
Simon Liu
  • 431
  • 5
  • 10
1

If you want a slick solution you can use my gem rearmed_rails which has a feature in it that safely monkey patches options_for_select and options_for_collection_select

rails g rearmed_rails:setup

Open config/initializers/rearmed_rails.rb and change the following values to true

options_for_select_include_blank: true,
options_from_collection_for_select_include_blank: true


Now whenever you need a blank included simply do the following:

<%= options_for_select(Model.all.map{|x| [x.name,x.id]}, include_blank: true) %>
Weston Ganger
  • 4,956
  • 2
  • 35
  • 37