Reputation: 545
This is a short one. I have a select box that is in a form used to create and edit a group. It works, saves the selected option to the database, but the form does not display what is in the database, it always shows the first option.
<%= form_for @group, :html => {:multipart => true} do |f| %>
<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], @group.privacy), {}, {:class => 'form-control'} %>
<%= f.submit :class => 'btn btn-primary' %>
<% end %>
I have also tried:
<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], :selected => @group.privacy), {}, {:class => 'form-control'} %>
Again, that save the data but does not display the selected option.
Thanks for any help you can offer.
Upvotes: 1
Views: 1019
Reputation: 1613
Use this code:
<%= form_for @group, :html => {:multipart => true} do |f| %>
<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], :selected => f.object.privacy), {}, {:class => 'form-control'} %>
<%= f.submit :class => 'btn btn-primary' %>
Upvotes: 0
Reputation: 3803
<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], :selected => f.object.privacy), {}, {:class => 'form-control'} %>
Try this
Upvotes: 2