2013-03-21 51 views
0

我有URL頁面我怎麼能添加參數給定的URL是在軌動態

http://localhost:3000/athletes/list 

當這個頁面就變成像

http://localhost:3000/athletes/list?first_name=sachin&last_name=tendulkar 

用戶的搜索中,first_namelast_name是用戶搜索時的搜索參數。

我想

http://localhost:3000/athletes/list?first_name=sachin&last_name=tendulkar&view_type=something 

我試圖其中添加參數view_type的鏈接=東西在URL for ex. 對於第一個URL

http://localhost:3000/athletes/list?view_type=something 

對於第二個URL以下

<%= link_to "Something ", :view_type => 'something' %> 

但對於這兩個網址,它提供了以下網址

http://localhost:3000/athletes/list?view_type=something 

回答

5
<%= link_to "Something ", params.merge(:view_type => 'something') %> 

雖然上面的代碼在大多數情況下適用於某種原因,它是給錯誤某些URL可能是因爲我使用friendly_url for ex.

的URL

http://localhost:3000/athlete/sachin_ramesh_tendulkar_1 

給我以下錯誤的網址

http://localhost:3000/athlete/1?view_type=something 

爲了固定此我使用javascript方法如下

function something(){ 
    var par ="" 
    var link =window.location.href 
    if (link.indexOf('view_type=something') == -1) 
     par = link.indexOf('?') != -1 ? "&view_type=something" : "?view_type=something" 
    window.location.href = link+par 
} 

和軌道代碼

<%= link_to "Something ", "javascript:void(0)", :onclick => "something();" %> 
相關問題