如何使 select_tag 保持上次搜索的值? [英] How to make the select_tag keep value of last search?

查看:44
本文介绍了如何使 select_tag 保持上次搜索的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 select_tag 的价格下拉列表,但是每次我使用下拉列表设置价格然后进行搜索时,当返回搜索结果时,下拉列表中的值会恢复为默认值.如何让下拉菜单停留在最后一个值?这是代码,我在最后得到了 :selected => 但不知道该赋予它什么值:

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

推荐答案

:selected 应该指向要预选的数组元素的最后一个元素.

:selected should point to the last element of the array element that you want to pre select.

试试

<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000] =>:; 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] =>:; params[:max])) %>

请参阅Rails API 上的options_for_select

这篇关于如何使 select_tag 保持上次搜索的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆