0

I have this helper for my user class:

<%= f.select :sex, options_for_select([['Male', 0], ['Female', 1], ['Other', 2], ['Undefined', 3]]), {}, {class:"form-control"} %>

When I load the form, everything is filled out except the :sex value. It defaults to the first.

Batman
  • 5,563
  • 18
  • 79
  • 155
  • 1
    Possible duplicate of [rails erb form helper options\_for\_select :selected](http://stackoverflow.com/questions/19120706/rails-erb-form-helper-options-for-select-selected) – Pavan Oct 07 '15 at 04:44

2 Answers2

2

the syntax is select options_for_select([value1, value2, value3], default) so in your case you can write this:

<%= f.select :sex, options_for_select([['Male', 0], ['Female', 1], ['Other', 2], ['Undefined', 3]], @user.sex), {}, {class:"form-control"} %>

assuming you have assigned @user.

mb_s88
  • 392
  • 1
  • 12
0

try this

<%= f.select :sex, options_for_select([['Male', 0], ['Female', 1], ['Other', 2], ['Undefined', 3]], ['Female', 1]), {}, {class:"form-control"} %>

the ['Female', 1] is the default value you set; you can change it :)

the syntax is

select options_for_select([value1, value2, value3], default)

This method will respect the value that is set in the database while creating the record.

Shiva
  • 11,485
  • 2
  • 67
  • 84