Reputation: 3614
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
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