'Rails Routes :shallow => true

Wondering why my /articles route is not coming up when I apply :shallow => true?

routes.rb

resources :users, :shallow => true do
  resources :articles
end

Also tried this:

resources :users do
  resources :articles, :shallow => true
end

Visiting /articles won't show me all articles from any user as expected, but I can still visit /articles/:id just fine. Is this expected behavior?



Solution 1:[1]

The shallow: true option does not provide an index resource according to the docs. Therefore, you will not be able to access just /articles. So yes, this is the expected behavior.

Solution 2:[2]

Yes - anything with an ID (show, edit, update, destroy) doesn't need a parent to identify itself.

The other two (create, index), need the parent to scope the query.

You would need to create another resource

resources :articles, :only => [:index]

To see all articles

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 katericata
Solution 2 Mark Swardstrom