2014-01-12 102 views
0

我正在嘗試獲取一系列工作鏈接。有些應該是可切換的,有些則不可以。鏈接和參數

基本上在頁面刷新我的PARAMES散列已經有兩個鍵,它不應該有,我不能看到他們在哪裏被添加。

我的鏈接:

<ul class="all filter__group"> 
    <li><%= link_to "View all", coasters_path(page: 1), remote: true %></li> 
</ul> 

<ul class="types filter__group"> 
    <%= content_tag(:li, class: ("active" if params[:type] == "steel")) do %> 
    <%= link_to "Steel", coasters_path(type_parameters("steel", page: 1)), remote: true %> 
    <% end %> 
    <%= content_tag(:li, class: ("active" if params[:type] == "wood")) do %> 
    <%= link_to "Wood", coasters_path(type_parameters("wood", page: 1)), remote: true %> 
    <% end %> 
    <%= content_tag(:li, class: ("active" if params[:type] == "powered")) do %> 
    <%= link_to "Powered", coasters_path(type_parameters("powered", page: 1)), remote: true %> 
    <% end %> 
</ul> 

<ul class="letters filter__group"> 
    <%= content_tag(:li, class: ("active" if params[:letters] == "#")) do %> 
    <%= link_to "#", coasters_path(letter_parameters("#", page: 1)), remote: true %> 
    <% end %> 
    <%= content_tag(:li, class: ("active" if params[:letters] == "a-e")) do %> 
    <%= link_to "A-E", coasters_path(letter_parameters("a-e", page: 1)), remote: true %> 
    <% end %> 
    <%= content_tag(:li, class: ("active" if params[:letters] == "f-j")) do %> 
    <%= link_to "F-J", coasters_path(letter_parameters("f-j", page: 1)), remote: true %> 
    <% end %> 
    <%= content_tag(:li, class: ("active" if params[:letters] == "k-o")) do %> 
    <%= link_to "K-O", coasters_path(letter_parameters("k-o", page: 1)), remote: true %> 
    <% end %> 
    <%= content_tag(:li, class: ("active" if params[:letters] == "p-t")) do %> 
    <%= link_to "P-T", coasters_path(letter_parameters("p-t", page: 1)), remote: true %> 
    <% end %> 
    <%= content_tag(:li, class: ("active" if params[:letters] == "u-z")) do %> 
    <%= link_to "U-Z", coasters_path(letter_parameters("u-z", page: 1)), remote: true %> 
    <% end %> 
</ul> 
<%= debug params %> 

的輔助方法:

# Add the type parameter to URL if not already there and remove if they are 
    def type_parameters(type, options = {}) 
    parameters = {} 
    if params[:type] == type 
     # Get exisiting params 
     parameters = params 
     # Remove type 
     parameters.delete(:type) 
     # Merge any parameters passed in via options 
     parameters.merge!(options) 
    else 
     # Get exisiting params 
     parameters = params 
     #Add type to new parameters variable 
     parameters.merge!(type: type) 
     # Merge any parameters passed in via options 
     parameters.merge!(options) 
    end 
    parameters 
    end 

    # Add the letters parameter to URL if not already there and remove if they are 
    def letter_parameters(letters, options = {}) 
    parameters = {} 
    if params[:letters] == letters 
     # Get exisiting params 
     parameters = params 
     # Remove letters 
     parameters.delete(:letters) 
     # Merge any parameters passed in via options 
     parameters.merge!(options) 
    else 
     # Get exisiting params 
     parameters = params 
     #Add letters to new parameters variable 
     parameters.merge!(letters: letters) 
     # Merge any parameters passed in via options 
     parameters.merge!(options) 
    end 
    parameters 
    end 

在一個頁面重新加載,這是當前PARAMS哈希:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
action: index 
controller: coasters 
type: powered 
page: 1 
letters: u-z 

在哪裏,我錯了任何想法?

+0

你能不能給我們的控制器操作的新副本? –

+0

控制器操作:https://gist.github.com/anonymous/8383369 – rctneil

+0

你確定沒有重新載入包含'letters = u-z'的URL嗎? –

回答

0

您正在覆蓋幫助器中的參數散列:當您使用merge!時,您將覆蓋原始的params散列。

這應該工作:

# Add the letters parameter to URL if not already there and remove if they are 
    def letter_parameters(letters, options = {}) 
    parameters = params.dup 

    if params[:letters] == letters 
     # Remove letters 
     parameters.delete(:letters) 
    else 
     # Add letters to new parameters variable 
     parameters.merge!(letters: letters) 
    end 

    # Merge any parameters passed in via options 
    parameters.merge(options) 
    end 

注意使用dup創建params哈希表

+0

但是在哪裏。據我所看到的,我創建了一個新的參數變量,它最終成爲一個散列,其中包含用作該鏈接單擊時參數的項目。這是不對的? – rctneil

+0

我已經添加了一個解釋和一些代碼,應該使它更清晰:) –

+0

好的,謝謝。是的,它現在正在工作。 – rctneil