Marty
Marty

Reputation: 2224

Options_for_select not working: dropdown box for boolean

I'm trying to include a boolean value as a dropdown box using the following code in my edit view:

<div class="col-md-8">
  <%= f.select :match, options_for_select([['On', true], ['Off', false]]), class: 'form-control input-md' %>
</div>

However I'm experiencing two problems:

1.It does not display the correct value. Even when the user's value is false, it still displays On.

2.It does not implement the styling. The inspector shows that it implements it as follows:

<div class="col-md-8">
  <select name="user[match]" id="user_match">
    <option value="true">On</option>
    <option value="false">Off</option>
  </select>
</div>

What am I doing wrong?

Upvotes: 1

Views: 1150

Answers (1)

Gaurav Gupta
Gaurav Gupta

Reputation: 1191

Do it like this:

<%= f.select :match, options_for_select([['On', true], ['Off', false]], selected: your_object.match),{}, {class: 'form-control input-md'} %>

Upvotes: 5

Related Questions