I have a dropdown of prices using a select_tag, but each time I use the dropdown to set a price and then do the search then the values in the dropdowns go back to their default value when the search results are returned. How do I make the dropdown stay at the last value? Here’s the code, I’ve got the :selected => at the end but don’t know what value to give it:
<%= label :price, 'max', "to $" %>
<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000]], :selected => ['$1,000,000', 1000000])) %>
:selectedshould point to the last element of the array element that you want to pre select.Try
<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000]], :selected => 1000000)) %>If you want to keep the value from your current request you may go for
<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000]], :selected => params[:max])) %>See options_for_select on the Rails API as well.