0

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>
  • Possible duplicate of [How can I set the default value for an HTML – Shelvacu Sep 02 '16 at 20:35
  • Do you use plain ERB or a framework like Ruby on Rails? – spickermann Sep 02 '16 at 20:42
  • I use Sinatra . ROR uses to much magic so you don't know whats actually going on behind the scenes. I got it doing what i want now but id love to here of a different way or if not ill post what i did do – Joseph McKenzie Sep 02 '16 at 20:43

1 Answers1

1

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>
spickermann
  • 100,941
  • 9
  • 101
  • 131