0

我的页面有 url

http://localhost:3000/athletes/list

当用户在此页面上搜索时,它会变为类似

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

wherefirst_namelast_name是用户搜索时的搜索参数。

for ex. 我想要一个在 url 中为第一个 URL添加参数 view_type=something 的链接

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

对于第二个网址

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

我试过以下

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

但是对于这两个网址,它都会提供以下网址

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

2 回答 2

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

尽管出于某种原因,上面的代码在大多数情况下都有效,但它对某些 url 给出错误可能是因为我正在使用friendly_urlfor ex.

网址

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();"  %>
于 2013-03-21T05:49:26.123 回答
1

这也可能有用(来自 API)

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to#343-link-to-some-url-with-current-params

于 2013-03-21T06:09:42.100 回答