Ruby on Rails:提交搜索表单后保持用户输入

时间:2012-08-03 14:45:12

标签: ruby-on-rails ruby search drop-down-menu

快速提问。提交搜索时,我的一些字段会在搜索完成后在文本字段或下拉菜单中保存用户输入,但对于其他字段,它将恢复为默认选项。如何让他们保存用户选择。

    Start date:
<%= select_tag "start_date_dd", options_for_select({
    "Select a period" => "",
      "3 days ago"   => DateTime.now.to_date - 3.days,   # =    259_200 sec.
      "1 week ago"   => DateTime.now.to_date - 1.week,   # =    604_800 sec.
      "1 month ago"  => DateTime.now.to_date - 1.month,  # =  2_592_000 sec.
      "6 months ago" => DateTime.now.to_date - 6.months, # = 15_552_000 sec.
      "1 year ago"   => DateTime.now.to_date - 1.year,   # = 31_557_600 sec.
    }, :selected=>params[:start_date_dd] )%></br>



Status :

<%= select(@projects, :status, Project.all.map {|p| [p.status]}.uniq, :prompt => "-Any-", :selected => params[:status]) %></br>

此处我的状态下拉框将用户输入保留在文本框中,但我的开始日期下拉菜单没有。任何解决方法。感谢

1 个答案:

答案 0 :(得分:3)

:selected =&gt; params [:status]是您的状态下拉框保留值的原因 你需要添加:selected =&gt; params [:start_date_dd]添加到标签