ruby-on-railspaginationkaminaricustom-url

Rails 4.2 with Kaminari 0.17.0 - how to change "?page=x" to "/x" (ie no query string)


Whilst I understand that Tim Berners Lee specified the use of ? for dynamic url segments, the client on my project doesn't want them.

So does anyone know how, using Kaminari for paging, I can change the routing to replace:

domain.com/searchresults?page=x

with

domain.com/searchresults/x

Obviously, this has to work when you click next page, prev page etc so that the url always changes x to the correct page number.


Solution

  • From the docs I missed that you can add a concern in the routes, like this:

    concern :paginatable do
      get '(page/:page)', :action => :index, :on => :collection, :as => ''
    end
    

    Then apply the concern:

    resources :my_resources, :concerns => :paginatable