Read more

Nested controller routes (Rails 2 and 3)

Dominik Schöler
August 21, 2013Software engineer at makandra GmbH

In order to keep the controllers directory tidy, we recently started to namespace controllers. With the :controller option you can tell Rails which controller to use for a path or resource. For nested resources, Rails will determine the view path from this option, too.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

That means the following code in routes.rb

resources :users do
  resource :profile, controller: 'users/profiles' #[1]
end

… makes Rails expect the following directory structure:

app/
  controllers/
    users/
      profiles_controller.rb
    users_controller.rb
  ...
  views/
    users/
      profiles/
        show.html.haml
      index.html.haml
      ...

Note:

  • the user profiles controller must be named Users::ProfilesController
  • [1] specifying the controller as 'user/...' would make Rails expect views in view/user/... (singular)

The better approache might be to use a module. See this card on how to namespace with modules

Posted by Dominik Schöler to makandra dev (2013-08-21 20:34)