Search code examples
ruby-on-railshelper

Include blank and default for collection_select helper


I would like to have a "select an option" option in my dropdown on my rails application.

I am using the collection_select helper tag and it looks something like this:

<%= collection_select(:country, :id, Country.order('name ASC'), :id,:name, {},{:class => "input-xlarge"}) %>

I want the default option of the drop-down to be "Select a country".


Solution

  • Use the include_blank option:

    <%= collection_select(:country, :id, Country.order('name ASC'),
          :id, :name, 
          { include_blank: 'Select a country' }, 
          { :class => "input-xlarge" }) %>
    

    See the official documentation here