2015-02-05 174 views
0

我有頁面列出了路徑並提供了基於文本的搜索 - 請參見當前版本http://www.trailsee.com/trails。我添加了一個表單來過濾搜索結果,例如通過困難,活動,長度,允許狗等。我有過濾器工作,但它不會加載之前使用過的參數在複選框字段中,用戶可以選擇多個項目,例如在搜索後允許騎自行車,允許遠足等。目前它只加載最大價值的參數。例如如果我有params難度= 0 &難度= 1 &難度2它只會加載難度爲2的頁面。從params填充複選框

具體來說:travel_type(radio),:難度(複選框),:狗(複選框)和:action_types(複選框)有問題。

這裏是使用參數的樣品URL 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' } 

回答

0

你重寫你的參數[:難度]變量與&difficulty=0&difficulty=1&difficulty=2,你必須給每個複選框一個獨特的參數變量名稱。

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' 
+0

謝謝!當我使用上面的代碼時,默認是在加載/重載時選中的複選框。我想要的是他們默認未選中,但是當他們填充他們的篩選器的選擇,我希望它重新加載他們的選擇。當我更新時,它也不會更改URL參數。它仍然難度= 0和難度= 1和難度= 2。我將如何改變上面這樣做? – dsick

+0

對不起!我修正了我的上面的代碼,以便url正確更新。也可以查看http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag瞭解更多信息 – Dontreadonme