Search code examples
ruby-on-railsdropdownhtml-select

How do I get my "Select a State" option to appear in my select menu?


I’m using Rails 4.2.7. I have this code for displaying a select menu in my view

<%= addr.select :state_id, @states.collect {|s| [ s.name, s.id ]}, {selected: [email protected]? && [email protected]? ? @user.address.state.id : nil, prompt: 'Select State'}, class: "selectField selectMenu form-control"  %>

Problem is, when the code is rendered, the “Select a State” option is not present. I included the “prompt:” as per the documentation so I’m not sure what else I should do to get the select option to appear. What renders is

<select class="selectField selectMenu form-control select-hidden" name="user[address][state_id]" id="user_address_state_id"><option value="3526">Alabama</option>
<option value="3556">Alaska</option>
<option value="3547" selected="selected">Arizona</option>
<option value="3510">Arkansas</option>
…
<option value="3554">Wyoming</option></select>

Solution

  • The prompt will only display when there is currently no option selected. If you want to display the "Select State" option anyway, you can use include_blank: 'Select State' instead. See http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html