For the code given below I wanted to keep the select box selected with the value that is passed.
But this doesn’t work:
@yrs =[2011,2010,2009,2008]
&
I was using a select box as part of a Search bar, so wanted to have the default first option selected on first presentation of the form, but then retain the chosen option thereafter. This works great:
<% styles = Styles.all.sort %>
<%= form_tag styles_path, :method => 'get' do %>
Search: style
<%= select_tag :search_style, options_for_select(styles, selected: params[:search_style]) %>
with colour
<%= text_field_tag :search_color, params[:search_color] %>
<%= submit_tag "Search" %>
<% end %>