Use shallow routes

suggest change

One way to avoid deep nesting is to generate the collection actions scoped under the parent, so as to get a sense of the hierarchy, but to not nest the member actions. In other words, to only build routes with the minimal amount of information to uniquely identify the resource, like this:

resources :articles, shallow: true do
  resources :comments
  resources :quotes
  resources :drafts
end

The shallow method of the DSL creates a scope inside of which every nesting is shallow. This generates the same routes as the previous example:

shallow do
  resources :articles do
    resources :comments
    resources :quotes
    resources :drafts
  end
end

scope has two options that customize shallow routes. :shallow_path prefixes member paths with the given parameter:

scope shallow_path: "sekret" do
  resources :articles do
    resources :comments, shallow: true
  end
end

List the generated routes with:

rails routes

On Rails 4, the equivalent command is rake routes.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents