代码之家  ›  专栏  ›  技术社区  ›  Tallboy

我如何在rails中形成一个接受的rel规范url?page=参数?

  •  2
  • Tallboy  · 技术社区  · 14 年前

    我尽量避免使用宝石,因为我的问题很简单。

    我的网站有以下内容?参数=

    "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,还放入?page=如果存在,但忽略所有其他参数?

    3 回复  |  直到 14 年前
        1
  •  5
  •   Tallboy    14 年前

    这似乎很正常

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

        2
  •  2
  •   Sylar    9 年前

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

    <head>
      <%= auto_discovery_link_tag(:atom, your_path('your-params'), { rel: "canonical" }) %>
    </head>
    
        3
  •  0
  •   Mikhail K.    8 年前

    在最简单的情况下应该有效:

    <link rel="canonical" href="<%= "#{request.protocol}#{request.host}#{request.path}" %>">

    如果您正在使用协议或希望在规范url中使用GET params,请不要忘记添加它。

    推荐文章