Rails routing without a resource name

My rails app has a section model and page model. The section contains many pages.

# section.rb
class Section < ActiveRecord::Base
    has_many :pages 
end

# page.rb
class Page < ActiveRecord::Base
    belongs_to :section 
end

Assuming I have a section with slug 'about', and this section has three pages with slug inserts, people, history, a URL with a typical routing might look something like this:

http://example.com/sections/about/pages/intro
http://example.com/sections/about/pages/people
http://example.com/sections/about/pages/history

What is the best way to configure my routes so that I can use these URLs:

http://example.com/about/intro
http://example.com/about/people
http://example.com/about/history
+4
source share
1 answer

To remove “sections” and “pages” from all routes for sectionsand pages, you can use:

resources :sections, path: '' do
  resources :pages, path: ''
end

: . , example, , routes.rb :

resources :sections, path: '' do
  resources :pages, path: ''
end
resources :examples
root 'home#index'

http://example.com/examples "" section, examples#index, http://example.com/ sections#index, home#index, , :

resources :examples
root 'home#index'
resources :sections, path: '' do
  resources :pages, path: ''
end
+5

Source: https://habr.com/ru/post/1539179/


All Articles