如何使select_tag保持上次搜索的值?

时间:2012-05-22 05:34:28

标签: ruby-on-rails

我使用select_tag有一个价格下拉列表,但每次我使用下拉列表设置价格然后进行搜索时,下拉列表中的值会在返回搜索结果时返回到其默认值。如何使下拉列表保持最后一个值?这是代码,我有:selected =>在最后,但不知道给它什么价值:

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

1 个答案:

答案 0 :(得分:4)

:selected应该指向您要预先选择的数组元素的最后一个元素。

尝试

<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000]], :selected => 1000000)) %>

如果您想保留当前请求的值,可以选择

<%= select_tag(:max, options_for_select([['$100,000', 100000], ['$200,000', 200000], ...['$20,000,000', 20000000]], :selected => params[:max])) %>

同样请参阅options_for_select on the Rails API