Joseph McKenzie Joseph McKenzie - 3 months ago 11
HTML Question

how to get my dropdown menu of states to autofill the correct information in a better way then i have

I have a dropdown menu of states and i have it auto populating the correct state and showing at the top of the list and removing the state from farther down in the list, but what I would like to have it do is just to go down to that state rather then showing at the top of the list and removing from the correct spot in the list of states.

<label for="state">State</label>
<select class="form-control" id="state" name="state">
<% options = ["Alabama","Alaska","Arizona","Arkansas","California","Colorado",
"Connecticut","Delaware","District Of Columbia",
"Florida","Georgia","Hawaii","Idaho","Illinois",
"Indiana","Iowa","Kansas","Kentucky","Louisiana",
"Maine","Maryland","Massachusetts","Michigan",
"Minnesota","Mississippi","Missouri","Montana",
"Nebraska","Nevada","New Hampshire","New Jersey",
"New Mexico","New York","North Carolina",
"North Dakota","Ohio","Oklahoma", "Oregon",
"Pennsylvania","Rhode Island","South Carolina",
"South Dakota","Tennessee","Texas","Utah","Vermont","Virginia",
"Washington","West Virginia","Wisconsin","Wyoming"]- [customerinfo[0]['state'] ]%>

<option value=<%= customerinfo[0]['state'] %>><%= customerinfo[0]['state'] %></option>
<% options.each do |option| %>
<option value=<%=option%>><%=option%></option>
<% end %>
</select>

Answer

Without any helpers you could write something like this (note the <%= ' selected' if option == customerinfo[0]['state'] %> part):

<label for="state">State</label>
  <select class="form-control" id="state" name="state">
  <% options = ["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District Of Columbia","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma", "Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"] %>

  <% options.each do |option| %>
    <option value="<%= option %>"<%= ' selected' if option == customerinfo[0]['state'] %>><%= option %></option>
  <% end %>
</select>