opticon
opticon

Reputation: 3614

Rails Nested Forms - Dropdown menu on edit

I have a form with nested fields, one of which is a drop-down menu:

<%= f.select :points, options_for_select([1, 0, -1]) %>

It works fine, but when I want to edit an item this field defaults to 1. How can I get the dropdown to select the saved value?

Upvotes: 0

Views: 937

Answers (1)

vee
vee

Reputation: 38645

The second parameter to options_for_select is the selected value so try setting that by:

<%= f.select :points, options_for_select([1, 0, -1], f.object.points) %>

or, you could pass the selected option as:

<%= f.select :points, options_for_select([1, 0, -1], selected: f.object.points) %>

Note that f.object has a reference to the current object for which this field is built so you can use it to get the appropriate attribute to default the selection to.

Upvotes: 1

Related Questions