31

I use simple_form in my app.

How do i give the blank value in my selects a different text than "" ?

I just find an option to include blank or not.

4 Answers 4

59

It depends on how you are constructing your options for select. If you're doing it like the code below, just pass a string into the :include blank.

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'Some text here'})

If you're setting the options with a options_for_select(), then you can do something like the following:

options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])

With the value="" being the second value in the array and the name that shows up in the dropdown being first. So in your case, you could change the second answer to look like this:

options_for_select([["Some text here", ""], ["Dollar", "$"], ["Kroner", "DKK"]])
2
  • 2
    This wasn't asked, but if you want the blank option not to be selectable you can add :include_blank => true, disabled: "", selected: "", placeholder: "Foo Bar"
    – MCB
    Commented Sep 23, 2014 at 15:06
  • Georgi has a better answer, below. instead of include_blank, use prompt: 'Which person?' It will have a blank value, but the display text you provide. include_blank only accepts a boolean -- it does not accept a string to display. Commented Dec 1, 2017 at 15:32
38

Instead of

:include_blank => true

Try

:include_blank => "your text here"

if this is what you are looking.

2
  • 1
    Although not the answer to the question - it was the answer to my question as I wanted to set the blank label - cheers. Commented Feb 20, 2014 at 15:05
  • This will not work for select_tag methods but will work with select methods. (And, yes, they are different.) Using a string for the value in the include_blank will only provide a blank option with no string. Use prompt in that case. Commented Jun 15, 2015 at 21:05
23

If you are using the select_tag(name, option_tags = nil, options = {}) function, the correct option is :prompt => "Some text" rather than setting a string value for select

4
  • 3
    Took me too long to figure out it is different for select_tag than a traditional select. Thanks.
    – zarazan
    Commented Feb 20, 2013 at 20:12
  • tag behaves so much differently all over the place. trips me up all the time, as well.
    – Dudo
    Commented Aug 23, 2014 at 3:46
  • 2
    This is absolutely correct and a bit tricky to understand. select_tag will accept a include_blank option but it will NOT display the text you pass as in as the value for that option. You need to use prompt for that. Thanks @Georgi! Commented Jun 15, 2015 at 21:03
  • Rails 6.1 prompt is working on select and not just select_tag.
    – notapatch
    Commented Mar 23, 2021 at 11:33
1

You can do this manually by adding ["Your Text", ""] to the beginning of the array passed to options_for_select, or add "<option value=\"\">#{h("Your Text"}</option>" to beginning of the string passed to select_tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.