12

I have a collection select like the following:

<%= f.collection_select :region_id, Region.find(:all), :id, :name, { :prompt => 'Select a State/Province' }, :style => "width: 200px;" %>

Sometimes the prompt from the :prompt option appears, but sometimes it does not. Does anyone know where I could begin to troubleshoot this? Maybe I have been looking at it too long...

Tony
  • 18,776
  • 31
  • 129
  • 193
  • Just to clarify: are you saying that the above exact code sometimes works and sometimes doesn't, or that you have two collection-selects with similarish code one one works and the other doesn't? – Taryn East Aug 31 '10 at 13:22

5 Answers5

43

:include_blank with the value of your blank option seems to do the trick. Try this:

 {:include_blank => "Please select"}
benjaminjosephw
  • 4,369
  • 3
  • 21
  • 40
  • 1
    Just a gotcha to be aware of: this will not respect the string passed into `include_blank` if you are using `select_tag` method. Instead, use `prompt`. – Joshua Pinter Jun 15 '15 at 19:37
13

:prompt appears in the list when there isn't a selected value.

:include_blank appears in the list always, even if you've loaded the select with a selected value.


if you want your select to always have "Select a State/Province" as the first option:

<%= f.collection_select :region_id, Region.all, :id, :name, include_blank: 'Select a State/Province' %>

if you want your collection to have "Select a State/Province" as the first option only when a region is not already selected:

<%= f.collection_select :region_id, Region.all, :id, :name, prompt: 'Select a State/Province' %>

source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

robotdana
  • 532
  • 4
  • 11
  • 1
    `:prompt` also appears always, but is not selected on page load, if there is a selected value. – Alexander Popov Dec 09 '14 at 13:24
  • @AlexPopov I don't believe that is true. It seems that if your object has a value for, in this case, `region_id` when the form loads, the prompt value will not be there, whereas `include_blank` will be there. – Joshua Pinter Jun 15 '15 at 19:38
  • @JoshPinter I am seeing the same problem: even after another value is selected, the prompt value is still a selectable option. – Eric Chen Feb 18 '17 at 04:17
  • @EricChen Interesting. That's definitely not how it is supposed to work. – Joshua Pinter Feb 21 '17 at 16:27
  • I'm facing this problem, anyone here could found a workaround? Thanks in advance. @EricChen – Sidonai Apr 22 '19 at 17:12
2

I've been having the same problem. Using 'prompt' seems to create an attribute for the select tag, problem is there is no such attribute that I know of. Plus its clearly not what is described in the Rails docs http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select.

Using include_blank seems a good workaround for me.

Cheers, Robin.

  • If it is creating attributes, then you have to surround prompt with braces, like `{prompt: true}`. This separates the options hash for the Rails helper method from the attributes hash for the HTML element. Only the very last hash parameter of a method can go without braces. – Chloe Jun 29 '17 at 17:52
0

try this <%= f.collection_select :region_id, Region.all, :id, :name, {prompt: 'Select a State/Province'}, {class: "form-control"} %>

Sudhir Vishwakarma
  • 785
  • 10
  • 15
  • 2
    Hi and welcome to SO. In future please add additional information when answering questions so that the OP and others in future can further understand your solution – Deepend Aug 19 '16 at 10:54
-1

Instead of

:prompt => "Select a State/Province"

try

:allow_blank => "Select a State/Province"

EDIT: Yes after checking the API I can see that I had it confused, prompt is the correct way according to the documentation, could it be that it only sometimes it appears because your object has a value already and therefore the prompt is there but it is not the currently selected value in the drop down list???

nitecoder
  • 5,496
  • 1
  • 28
  • 35
  • thank you for verifying. however, the prompt does not even appear on the list when there is a selected value. it is non-existent. for now, i have hacked it in JS, but i am a bit confused as to what is going on... – Tony Apr 19 '09 at 06:49
  • It's `:include_blank`. http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html – Chloe Jun 29 '17 at 18:00