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?