How do I set a blank value for an f.select form field

Ruby on-RailsRubySelect

Ruby on-Rails Problem Overview


I am using the following to allow my users to select their sex in their profile.

<%= f.select (:sex, %w{ Male Female }) %>

How would I create a blank value that the list would default to if nothing has been passed to the user.sex column? I am simply passing male or female as a string.

The purpose is I want a blank value so a validation can make sure they are aware they have to select it.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

There are two possibilities, depending on what you're after:

include_blank
<%= f.select (:sex, %w{ Male Female }, :include_blank => true) %>

This will always include a blank option in the select, which will allow people to set the value back to the blank value if they're seeing this on an edit form.

prompt
<%= f.select (:sex, %w{ Male Female }, :prompt => "Gender...") %>

This will include the specified prompt value, so long as the field hasn't already been set. If it has (on an edit form for example), there's no need to remind the user that they need to select a value so the prompt doesn't appear

Solution 2 - Ruby on-Rails

I think you can do something like this:

<%= f.select (:sex, %w{ Male Female }, {:include_blank => 'None Specified'} ) %>

Solution 3 - Ruby on-Rails

in Rails 4 you can achieve prompt like this:

<%= f.select :gender, %w{ Male Female }, {:prompt => "Gender..."} %>

its working for me with simple form.

Solution 4 - Ruby on-Rails

You go with

<%= f.select :gender, %w{ Male Female }, :include_blank => true %>

Solution 5 - Ruby on-Rails

I tried to use 'prompt' as a string. But in the rendered output, the new option prompt was not appearing. The select_tag method searches only for a symbol. It appears to be the case for :include_blank as well. Check out the options.delete:

  def select_tag(name, option_tags = nil, options = {})
    option_tags ||= ""
    html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name

    if options.include?(:include_blank)
      include_blank = options.delete(:include_blank)

      if include_blank == true
        include_blank = ''
      end

      if include_blank
        option_tags = content_tag(:option, include_blank, value: '').safe_concat(option_tags)
      end
    end

    if prompt = options.delete(:prompt)
      option_tags = content_tag(:option, prompt, value: '').safe_concat(option_tags)
    end

    content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
  end

Also note that :include_blank and :prompt will be options of the select or select_tag, not the options_for_select.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbgadociView Question on Stackoverflow
Solution 1 - Ruby on-RailsGarethView Answer on Stackoverflow
Solution 2 - Ruby on-RailsjyosephView Answer on Stackoverflow
Solution 3 - Ruby on-RailsZiv GaliliView Answer on Stackoverflow
Solution 4 - Ruby on-RailsHardik HardiyaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsDaniel ViglioneView Answer on Stackoverflow