As per the docs, it's the prompt
option in the options
argument:
collection_select(:post, :author_id, Author.find(:all),
:id, :name_with_initial,
{:prompt => 'Please select the author of this post'})
With collection_select
on a form builder we omit the first argument, so in this case:
f.collection_select :country_id, Country.order(:name), :id, :name, {prompt: 'Select your country'}
I've 100% confirmed this as working on my own app running Rails 4.1.6, where prompt
and include_blank
do the same thing.
The way this works is Rails injects a null-valued <option>
as the first item in the generated <select>
(this is because the HTML spec has nothing analogous to placeholder
on text inputs for select inputs).
Reasons this may fail:
- Rails does not mark the prompt option with the
selected
attribute, and I suspect some browsers may choose to render their own blank entry instead of the first in the list
- If, for existing records, Rails determines that the current record's
country_id
matches an element in the list it will mark that one as selected
. This is expected behaviour but can be a pain if you're doing anything non-standard.
If you're being bitten by these problems your options are to build the form manually (the method options_from_collection_for_select
may be of use here) or do it in javascript. There is also an undocumented default
attribute you can add to an <option>
tag but it's not in the spec and browser support may be patchy, and you'd still have to build the form manually.