skip to Main Content

I wrote an application in rails 4. In that app, I have two pagination in single page ‘x (page)’. Params like groups and page in the 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.

I am using Kaminari gem to do pagination. In that gem I used rel_next_prev_link_tags helper to show link tag for prev/next.

How to show link tags for multiple pagination?

2

Answers


  1. Chosen as BEST ANSWER

    I created an custom helper to process the URL and based on params create the categorized link tags. ex: In view,

    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
    

  2. You can’t show search engines two-dimensional pagination. In your case it looks more like grouping/categorizing + pagination.

    Like:

    Group 1 pages:
    https://example.com/x
    https://example.com/x?page=2
    https://example.com/x?page=3
    Group 2 pages:
    https://example.com/x?group=2
    https://example.com/x?page=2&group=2
    https://example.com/x?page=3&group=2
    

    Etc.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search