How to make the select_tag keep value of last search?

前端 未结 1 547
臣服心动
臣服心动 2021-01-24 18:26

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

相关标签:
1条回答
  • 2021-01-24 19:05

    :selected should 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.

    0 讨论(0)
提交回复
热议问题