Sal*_*lil 0 ruby parameters ruby-on-rails link-to ruby-on-rails-3
我有网页有网址
http://localhost:3000/athletes/list
Run Code Online (Sandbox Code Playgroud)
当用户在此页面上搜索时,它会变为类似的内容
http://localhost:3000/athletes/list?first_name=sachin&last_name=tendulkar
Run Code Online (Sandbox Code Playgroud)
凡first_name和last_name是搜索参数时,用户的搜索.
我想要一个链接,在url中for ex.
为第一个URL 添加参数view_type = something
http://localhost:3000/athletes/list?view_type=something
Run Code Online (Sandbox Code Playgroud)
对于第二个URL
http://localhost:3000/athletes/list?first_name=sachin&last_name=tendulkar&view_type=something
Run Code Online (Sandbox Code Playgroud)
我试过以下
<%= link_to "Something ", :view_type => 'something' %>
Run Code Online (Sandbox Code Playgroud)
但对于这两个网址,它会提供以下网址
http://localhost:3000/athletes/list?view_type=something
Run Code Online (Sandbox Code Playgroud)
<%= link_to "Something ", params.merge(:view_type => 'something') %>
Run Code Online (Sandbox Code Playgroud)
虽然上面的代码在大多数情况下都适用于某种原因,但是为某些网址提供错误可能是因为我使用的是friendly_url for ex.
为网址
http://localhost:3000/athlete/sachin_ramesh_tendulkar_1
Run Code Online (Sandbox Code Playgroud)
给我以下错误的网址
http://localhost:3000/athlete/1?view_type=something
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我使用的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
}
Run Code Online (Sandbox Code Playgroud)
和rails代码
<%= link_to "Something ", "javascript:void(0)", :onclick => "something();" %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
867 次 |
| 最近记录: |