mattC
mattC

Reputation: 373

Rails : Drop down in simple_form doesn't retain value when directed to edit path

I have a drop down selector in a form and even after saving an object, when I go to my edit page, the drop down reverts to the first item. If submit is clicked, the value changes to the first item in the list.

In this case the drop down contains a list of states. Every time I go to the edit page, Alabama is selected and if I don't manually change the value back to what it initially was, the state becomes Alabama.

<%= simple_form_for @event, url: coin_event_path(@coin.id) do |f| %>    
<%= f.input :content, :label => "Event Description", class: 'form-control' %>
<%= f.input :link, :label => "Link to Event", class: 'form-control' %>
<%= f.input :date, order: [:month, :day, :year], class: 'form-control' %>
<%= f.input :time, as: :time, html5: true, class: 'form-control' %>
<%= f.input :city, class: 'form-control' %>
<%= f.select :state, options_for_select(us_states),{}, class: 'form-control' %>
<%= f.input :description, :label => "Event Description", class: 'form-control' %>
<% if can? :destroy, Event %>
     <%= f.select :accepted, [['Accepted', true], ['Not Accepted', false]] %>
<% end %>
<%= f.button :submit, 'Submit' %>

<%= link_to "Back", coin_path(@coin.id), class: "btn btn-default" %>

How do I change this so it stays on the state that it's supposed to?

Upvotes: 0

Views: 128

Answers (1)

Michael Arkhipov
Michael Arkhipov

Reputation: 765

You can using selected, like:

options_for_select(us_states, selected: "set_current_value")

More usage examples - options_for_select() docs.

Upvotes: 1

Related Questions