raphael_turtle
raphael_turtle

Reputation: 7314

Populate select box in rails form on edit page

I have a rails form with a select box and various options I want the user to choose from. _form.html.slim

.form-group
        = f.label :year
        = f.select :year, options_for_select(["one", "two", "three", "four"]),
         {class: 'form-control'}

The user chooses an option and submits the from which is saved but when editing an item the chosen option is not automatically selected whereas the other fields in the form are...what am I doing wrong?

Upvotes: 1

Views: 1096

Answers (2)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

You don't need the options_for_select. the select method will generate that for you.

= f.select :year, ["one", "two", "three", "four"], {}, {:class => 'form-control'}

The empty {} is for the select options (you can put in :include_blank or :prompt for example). The second options hash is for html options like class

Upvotes: 1

SteveTurczyn
SteveTurczyn

Reputation: 36860

specify the selected value by using

   = f.select :year, options_for_select(["one", "two", "three", "four"], selected: f.object.year), {class: 'form-control'}

Upvotes: 3

Related Questions