将下拉列表重置为所选值

时间:2014-01-10 19:15:42

标签: javascript jquery ruby-on-rails

我有一个索引页面,显示数据库中的所有行程。页面顶部是一个带下拉菜单的过滤器,您可以选择类别,区域或两者来过滤结果。

当您选择自己的选择并点击“显示”时下拉菜单返回到“所有类别”的默认选项。和'所有地区'。

如何在筛选结果时在下拉列表中显示所选选项?

这是我的下拉菜单:

.row
  = form_tag all_road_trips_path, id: 'filter-trips-form', method: :get do
  .select-intro
    Browse
  .select-wrapper.trip-categories
    = collection_select(:category, :id, Category.all, :id, :name, :prompt => "All categories")
  .select-intro
    trips for
  .select-wrapper
    = collection_select(:region, :id, Region.all, :id, :name, :prompt => "All regions")
  .select-button
    = submit_tag 'Show Trips', class: 'button square'

EDIT ::

我尝试添加selected值并获得了一个未定义的方法`[]'为零:NilClass'错误。我认为这是因为我的下拉的第一个价值是'所有类别'并且实际上并不是db中的对象,因此缺少id。这是更新的代码和服务器输出:

.row
  = form_tag all_road_trips_path, id: 'filter-trips-form', method: :get do
  .select-intro
    Browse
  .select-wrapper.trip-categories
    = collection_select(:category, :id, Category.all, :id, :name, :prompt => "All categories", :selected => params[:category][:id])
  .select-intro
    trips for
  .select-wrapper.trip-regions
    = collection_select(:region, :id, Region.all, :id, :name, :prompt => "All regions", :selected => params[:region][:id])
  .select-button
    = submit_tag 'Show Trips', class: 'button square'

Started GET "/road-trips/all" for 127.0.0.1 at 2014-01-10 14:03:39 -0600
Processing by RoadTripsController#all as HTML
  Category Load (0.4ms)  SELECT "categories".* FROM "categories"
  Rendered road_trips/_trip_filter.html.haml (4.7ms)
  Rendered road_trips/all.html.haml within layouts/application (5.6ms)
Completed 500 Internal Server Error in 9ms
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1

NoMethodError - undefined method `[]' for nil:NilClass:
  app/views/road_trips/_trip_filter.html.haml:6:in `block in _app_views_road_trips__trip_filter_html_haml__2571960046883419269_70320079055780'
  haml (3.1.8) lib/haml/helpers/action_view_mods.rb:162:in `block (2 levels) in form_tag_with_haml'

1 个答案:

答案 0 :(得分:2)

使用:selected选项:

= collection_select(:category, :id, Category.all, :id, :name, :prompt => "All categories", :selected => params[:category_id or however you store this value])

在这里的底部评论中简要讨论:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

发生了什么:通过将值传递给:selected表单助手的collection_select选项,您告诉它在值匹配时将其中一个选项标记标记为已选中。例如,给出选项'apple','banana','chocolate':

<select name="treat">
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="chocolate">Chocolate</option>
</select>

当您将'chocolate'传递给collection_select表单助手时,它会将值与选项标记的值匹配,并将匹配的标记标记为已选择:

= collection_select :treat, args..., :selected => 'chocolate'
# will render:
<select name="treat">
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="chocolate" selected>Chocolate</option>
</select>

希望有所帮助。

修改 您正在调用params值,而不是:

params[:category][:id]

它应该是:

params[:category_id]

这也适用于另一个参数。

这是因为表单助手:collection_select :category, :id会生成一个名为category_id的select标记,该标记将是提交的参数。

错误no method [] for nilClass是由params[:category]返回nil引起的,因为没有这样的参数。