rails select_tag selected value

后端 未结 4 1709
孤街浪徒
孤街浪徒 2020-12-23 19:24

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]
&         


        
4条回答
  •  不思量自难忘°
    2020-12-23 19:25

    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 %>

提交回复
热议问题