There are different ways to nest controllers, depending whether the route, the controller or both should be nested.

Nest only controller

In routes.rb:

scope module: :cats do
  resources :friends
end
  • Controller: Cats::FriendsController
  • Routes: /friends
  • Index URL helper: friends_path

Nest only routes

scope :cats do
  resources :friends
end
  • Controller: FriendsController
  • Routes: /cats/friends
  • Index URL helper: friends_path

Nest both controller and routes

scope :cats, module: :cats do
  resources :friends
end
  • Controller: Cats::FriendsController
  • Routes: /cats/friends
  • Index URL helper: friends_path

Namespace

A namespace nests both controller and routes. However, it also includes the namespace in the URL helpers.

namespace :cats do
  resources :friends
end
  • Controller: Cats::FriendsController
  • Routes: /cats/friends
  • Index URL helper: cats_friends_path