从params填充复选框

时间:2015-02-05 00:44:19

标签: ruby-on-rails checkbox params slim-lang

我有一个列出路径并提供基于文本的搜索的页面 - 请参阅此处http://www.trailsee.com/trails的当前版本。我添加了一个表单来过滤搜索结果,例如通过难度,活动,长度,允许狗等。我有过滤器工作,但它没有在用户可以选择多个项目的复选框字段中加载以前使用的参数允许骑自行车,在搜索后允许徒步旅行等。它目前只加载最大值的参数。例如如果我有params难度= 0&难度= 1& difficulty2它只会加载选择难度为2的页面。

具体来说:travel_type(收音机),:难度(复选框),:狗(复选框)和:action_types(复选框)有问题。

以下是带有参数的示例网址 http://localhost:3000/trails?utf8=%E2%9C%93&travel_type=on&max_time=168&max_distance=35&difficulty=0&difficulty=1&difficulty=2&dogs=1&dogs=2&action_types=1&action_types=2&action_types=3&commit=Filter

重新加载 travel_type 2被选中 难度2被选中 狗2被选中 选择了action_types 3

这是我用苗条

编写的视图代码
.page
  section.search
    .row
      form.form-inputs.columns.large-6.large-offset-1#search-form action=trails_path method='get'
        input type='search' placeholder='Search' name='query_string'
        button.button-search
          i.sprite-icon-search
  section.filter
    .row
      = form_tag nil, method: :get, class: 'form-inputs columns large-11 large-offset-1', id: 'trail-filter-form' do
        .estimate_time 
          strong Estimate time using
          ul
            li
              = radio_button_tag :travel_type, params[:travel_type], 0
              = label_tag 'Hiking'
            li
              = radio_button_tag :travel_type, params[:travel_type], 1
              = label_tag 'Running'
            li
              = radio_button_tag :travel_type, params[:travel_type], 2
              = label_tag 'Biking'
        div.max_time
          strong Max Time
          div.time
            p
              = '30m'
              = range_field_tag :max_time, params[:max_time], in: 30..240
              = '4+ hours'
            p
          strong Max Distance
          div.distance
            p
              = '1mi '
              = range_field_tag :max_distance, params[:max_distance], in: 1..50
              = '50+ mi'
        div.difficulty
          strong Difficulty
          div
          p
            = check_box_tag :difficulty, 0, params[:difficulty] == '0'
            = label_tag 
              i class='sprite-icon-difficulty-0'
            = check_box_tag :difficulty, 1, params[:difficulty] == '1'
            = label_tag 
              i class='sprite-icon-difficulty-1'
            = check_box_tag :difficulty, 2, params[:difficulty] == '2'
            = label_tag 
              i class='sprite-icon-difficulty-2'
          p
          strong Dogs
          div
            = check_box_tag :dogs, 0, params[:dogs] == '0'
            = label_tag 'Off Leash'
            = check_box_tag :dogs, 1, params[:dogs] == '1'
            = label_tag 'w/Leash'
            = check_box_tag :dogs, 2, params[:dogs] == '2'
            = label_tag 'Not Allowed'
        div.views
          strong Views
          div
            = check_box_tag :views, 1, params[:views] == '1'
            = label_tag 'Has Views'
          p
          strong Activities Allowed
          div
            = check_box_tag :action_types, 0, params[:action_types] == '0'
            = label_tag 
              i class='sprite-icon-horseback-med'
            = check_box_tag :action_types, 1, params[:action_types] == '1'
            = label_tag 
              i class='sprite-icon-ski-med'
            = check_box_tag :action_types, 2, params[:action_types] == '2'
            = label_tag 
              i class='sprite-icon-hiking-med'
            = check_box_tag :action_types, 3, params[:action_types] == '3'
            = label_tag 
              i class='sprite-icon-bike-med'
        = submit_tag 'Submit', value: 'Filter', class: 'button small right trail-filter'

  section.content#trails-find
    .row.find-trails-map
      .large-11.large-offset-1.columns
        .map#find-map
    = render 'trails/trail_list', locals: { decorated_trails: decorated_trails,
                                                 title: 'PUBLIC TRAILS' }

1 个答案:

答案 0 :(得分:0)

你用&difficulty=0&difficulty=1&difficulty=2覆盖你的params [:difficulty]变量,你必须给每个复选框一个唯一的params变量名。

 div.difficulty
      strong Difficulty
      div
      p
        = check_box_tag :difficulty0, true, false 
        = label_tag 
          i class='sprite-icon-difficulty-0'
        = check_box_tag :difficulty1, true, false
        = label_tag 
          i class='sprite-icon-difficulty-1'
        = check_box_tag :difficulty2, true, false
        = label_tag 
          i class='sprite-icon-difficulty-2'