我在rails 4中编写了一个应用程序。在那个应用程序中,我在单页“x(page)”中有两个分页。参数类似于url中的组和页面。

Url looks like:
https://example.com/x?page=2&group=4
Initial page:
https://example.com/x
If pagination page params, then
https://example.com/x?page=2
If paginating groups params, then
https://example.com/x?group=2
If paginating both,then
https://example.com/x?page=2&group=2
and so on.

我用卡米纳里宝石做分页。在那个gem中,我使用rel_next_prev_link_tags助手来显示prev/next的链接标记。
如何显示多页的链接标记?

最佳答案

我创建了一个自定义助手来处理url,并基于params创建分类链接标记。考虑到,

pagination_link_tags(@pages,'page') for pages pagination
pagination_link_tags(@groups,'group') for groups pagination

def pagination_link_tags(collection,pagination_params)
    output = []
    link = '<link rel="%s" href="%s"/>'
    url = request.fullpath
    uri = Addressable::URI.parse(url)
    parameters = uri.query_values
    # Update the params based on params name and create a link for SEO
    if parameters.nil?
      if collection.next_page
        parameters = {}
        parameters["#{pagination_params}"] = "#{collection.next_page}"
        uri.query_values = parameters
        output << link % ["next", uri.to_s]
      end
    else
      if collection.previous_page
        parameters["#{pagination_params}"] = "#{collection.previous_page}"
        uri.query_values = parameters
        output << link % ["prev", uri.to_s]
      end
      if collection.next_page
        parameters["#{pagination_params}"] = "#{collection.next_page}"
        uri.query_values = parameters
        output << link % ["next", uri.to_s]
      end
    end
    output.join("\n").html_safe
  end

08-25 10:10