2

我試圖避免使用寶石,因爲我的問題很簡單。如何在接受?page = param的rails中構建一個rel規範的url?

我的網站具有以下?PARAMS =

"type" just filters by type. if this isnt specified they're all lumped together 
"sort" sorts by date/whats hot/latest 
"view" changes compact view/list view 
"scroll" isnt even used yet, its for infinite scroll on or off which i havent put in yet 
"reset" is a link when you click the logo (not the home link) that resets all session params to "all" and "whats hot" (the default) 
"page" is the pagination which i think is the only really important one 

得到一些幫助後,我被告知?page=是唯一真正重要的一個,因爲我所有的內容可以是沒有任何查詢參數的訪問,除非分頁。

所以現在我有這樣的:

<link rel="canonical" href="<%= "http://#{request.host + request.fullpath}" %>"> 

所以我的問題是如何修改這個所以它只會將URL,並還將頁=如果存在,但忽略了其他所有PARAMS?

回答

3

這似乎WROK

<link rel="canonical" href="<%= "http://#{request.host + url_for(:page => params[:page]) }" %>">

2

這是2017年,我知道,你也可以使用Rails的Auto Discovery Link

<head> 
    <%= auto_discovery_link_tag(:atom, your_path('your-params'), { rel: "canonical" }) %> 
</head>