Rich Coy
Rich Coy

Reputation: 545

Rails 4 Select Not Displaying Selected

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

Answers (2)

Uday kumar das
Uday kumar das

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

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], :selected => f.object.privacy), {}, {:class => 'form-control'} %>

Try this

Upvotes: 2

Related Questions