In Ruby on Rails, applications often have duplicate sections, for example, many sections have page-by-page navigation on index pages that you would like to set not with a GET parameter, but more beautifully, for example,

/catalogs или /catalogs/page/1 - первая страница со списком каталогов /catalogs/page/2 - вторая страница со списком каталогов /pages или /pages/page/1 - первая страница со списком страниц /pages/page/2 - вторая страница со списком страниц 

As a result, in config/routes.rb in addition to the resource definition of routes, additional get routes appear that specify the format of the index page and an optional parameter :page

 Rails.application.routes.draw do ... get 'catalogs(/page/:page)', to: 'catalogs#index', page: /\d+/ resources :catalogs, except: :index get 'pages(/page/:page)', to: 'page#index', page: /\d+/ resources :pages, except: :index ... end 

The rake routes command leads to the following output.

  GET /catalogs(/page/:page)(.:format) catalogs#index {:page=>/\d+/} catalogs POST /catalogs(.:format) catalogs#create new_catalog GET /catalogs/new(.:format) catalogs#new edit_catalog GET /catalogs/:id/edit(.:format) catalogs#edit catalog GET /catalogs/:id(.:format) catalogs#show PATCH /catalogs/:id(.:format) catalogs#update PUT /catalogs/:id(.:format) catalogs#update DELETE /catalogs/:id(.:format) catalogs#destroy GET /pages(/page/:page)(.:format) page#index {:page=>/\d+/} pages POST /pages(.:format) pages#create new_page GET /pages/new(.:format) pages#new edit_page GET /pages/:id/edit(.:format) pages#edit page GET /pages/:id(.:format) pages#show PATCH /pages/:id(.:format) pages#update PUT /pages/:id(.:format) pages#update DELETE /pages/:id(.:format) pages#destroy 

Is there a way to get exactly the same routes without defining a separate get root for the index page?

    1 answer 1

    Route can be used to solve the problem.

     Rails.application.routes.draw do ... concern :pageable do collection { get 'page/:page', action: :index, page: /\d+/ } end resources :catalogs, concerns: :pageable resources :pages, concerns: :pageable ... end 

    In this case, the rake routes command will contain routes that give the same result.

      GET /catalogs/page/:page(.:format) catalogs#index {:page=>/\d+/} catalogs GET /catalogs(.:format) catalogs#index POST /catalogs(.:format) catalogs#create new_catalog GET /catalogs/new(.:format) catalogs#new edit_catalog GET /catalogs/:id/edit(.:format) catalogs#edit catalog GET /catalogs/:id(.:format) catalogs#show PATCH /catalogs/:id(.:format) catalogs#update PUT /catalogs/:id(.:format) catalogs#update DELETE /catalogs/:id(.:format) catalogs#destroy GET /pages/page/:page(.:format) pages#index {:page=>/\d+/} pages GET /pages(.:format) pages#index POST /pages(.:format) pages#create new_page GET /pages/new(.:format) pages#new edit_page GET /pages/:id/edit(.:format) pages#edit page GET /pages/:id(.:format) pages#show PATCH /pages/:id(.:format) pages#update PUT /pages/:id(.:format) pages#update DELETE /pages/:id(.:format) pages#destroy 
    • Nothing like that. It may be worthwhile to fasten for :page value 1 by default. Although the partition libraries I used successfully understood nil as 1, this can be a good example. - D-side