Include blank for first item in select list in options_for_select tag

Ruby on-RailsRuby on-Rails-3ErbHtml Select

Ruby on-Rails Problem Overview

I tried :include_blank => true, but it didn't work.

    <%= options_for_select Model.all.collect{|mt| [,]} %>

If I need to add it to the collection, how would you do that?

Ruby on-Rails Solutions

Solution 1 - Ruby on-Rails

I think you want this format:

select("model_name", "model_id", Model.all.collect {|mt| [, ] }, {: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)

Solution 2 - Ruby on-Rails

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| [,]}.insert(0, "") %>

Solution 3 - Ruby on-Rails

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, options_for_select(Model.all.collect{|mt| [,]}), :include_blank => true %>

Solution 4 - Ruby on-Rails

<%= options_for_select Model.all.collect{|x| [,]}.unshift(["",nil]) %>

Solution 5 - Ruby on-Rails

= select_tag "some_value", options_for_select(Model.all.collect{ |x| [,]}.prepend([t('helpers.some_name'), nil]), :selected => params[:some_value])

Solution 6 - Ruby on-Rails

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({|x| [,]}, include_blank: true) %>

Solution 7 - Ruby on-Rails

You can use the following monkey patch to add the include_blank argument to options_for_select

module OptionsForSelectIncludeBlankPatch

  def options_for_select(container, selected = nil)
    if selected.is_a?(Hash)
      include_blank = selected[:include_blank] || selected['include_blank']

    options = super

    if include_blank
      include_blank = '' if include_blank == true

      if Rails::VERSION::MAJOR >= 5 && Rails::VERSION::MINOR >= 1
        str = tag_builder.content_tag_string(:option, include_blank, {value: ''})
        str = content_tag_string(:option, include_blank, {value: ''})




ActiveSupport.on_load(:action_view) do
  ActionView::Base.send(:include, RearmedRails::RailsHelpers)


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
QuestionB SevenView Question on Stackoverflow
Solution 1 - Ruby on-RailsChris BarrettoView Answer on Stackoverflow
Solution 2 - Ruby on-RailsDylan MarkowView Answer on Stackoverflow
Solution 3 - Ruby on-RailsPaulo FidalgoView Answer on Stackoverflow
Solution 4 - Ruby on-RailsWeston GangerView Answer on Stackoverflow
Solution 5 - Ruby on-RailsSimon LiuView Answer on Stackoverflow
Solution 6 - Ruby on-RailsWeston GangerView Answer on Stackoverflow
Solution 7 - Ruby on-RailsWeston GangerView Answer on Stackoverflow