【问题标题】:How can i add parameter to the given url which is dynamic in rails如何将参数添加到在rails中动态的给定url
【发布时间】:2013-03-21 05:40:56
【问题描述】:

我的页面有 url

http://localhost:3000/athletes/list

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

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

其中first_namelast_name是用户搜索时的搜索参数。

我想要一个在 url for ex. 中添加参数 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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 parameters link-to


    【解决方案1】:
    <%= link_to "Something ", params.merge(:view_type => 'something')  %>
    

    尽管由于某种原因,上面的代码在大多数情况下都有效,但它对某些 url 给出错误可能是因为我使用了friendly_url for 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
    }
    

    和rails代码

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

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2020-03-10
      • 2020-09-05
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多