ruby-on-railsrails-routing

Rails 6 - Issue with nested resources in a namespace


So I am trying to nest resources under a namespace, however when i try to navigate to the UserProfile new page I am hitting the following error:

ActionController::RoutingError at /users/xxxxxx/user_profiles/new
uninitialized constant AccountManagementPages::UserProfilesController
Did you mean?  AccountManagementPages::UsersController

This is how the resources are set up in my routes.rb file

  constraints(AccountManagement) do
    namespace :account_management_pages, path: '' do
      root to: 'users#new', as: :registration
      resources :users, except: %w[index], path_names: { new: 'register' } do
        resources :user_profiles
      end
    end
  end

my file structure for both my controller and views are configured correctly (at least I thought they were).

image of controller nesting for namespaced routes

And here is how my views are nested.

Image of view nesting in relation to controller nesting above

This is how I have my user_profiles_controller configured:

module AccountManagementPages
  module Users
    class UserProfilesController < ApplicationController

      def show; end

      def new; end

      def edit; end

      def create; end

      def update; end

    end
  end
end

and my model associations (don't think this is overly relevant here but just incase it is.)

class User < ApplicationRecord has_one :user_profile, dependent: :destroy end

class UserProfile < ApplicationRecord belongs_to :user end

any help here would be greatly appreciated. Not sure why I am hitting this error?

Thanks in advance.


Solution

  • If you do rails routes, you'll get (amongst other things):

                                     Prefix Verb   URI Pattern                                                        Controller#Action
              account_management_pages_registration GET    /                                                          account_management_pages/users#new
        account_management_pages_user_user_profiles GET    /users/:user_id/user_profiles(.:format)                    account_management_pages/user_profiles#index
                                                    POST   /users/:user_id/user_profiles(.:format)                    account_management_pages/user_profiles#create
     new_account_management_pages_user_user_profile GET    /users/:user_id/user_profiles/register(.:format)           account_management_pages/user_profiles#new
    edit_account_management_pages_user_user_profile GET    /users/:user_id/user_profiles/:id/edit(.:format)           account_management_pages/user_profiles#edit
         account_management_pages_user_user_profile GET    /users/:user_id/user_profiles/:id(.:format)                account_management_pages/user_profiles#show
                                                    PATCH  /users/:user_id/user_profiles/:id(.:format)                account_management_pages/user_profiles#update
                                                    PUT    /users/:user_id/user_profiles/:id(.:format)                account_management_pages/user_profiles#update
                                                    DELETE /users/:user_id/user_profiles/:id(.:format)                account_management_pages/user_profiles#destroy
                     account_management_pages_users POST   /users(.:format)                                           account_management_pages/users#create
                  new_account_management_pages_user GET    /users/register(.:format)                                  account_management_pages/users#new
                 edit_account_management_pages_user GET    /users/:id/edit(.:format)                                  account_management_pages/users#edit
                      account_management_pages_user GET    /users/:id(.:format)                                       account_management_pages/users#show
                                                    PATCH  /users/:id(.:format)                                       account_management_pages/users#update
                                                    PUT    /users/:id(.:format)                                       account_management_pages/users#update
                                                    DELETE /users/:id(.:format)                                       account_management_pages/users#destroy
    

    As you can see, user_profiles is not nested under the users namespace. Rails, therefore, is expecting:

    module AccountManagementPages
      class UserProfilesController < ApplicationController
    
        ...
    
      end
    end
    

    If you do:

    constraints(AccountManagement) do
      namespace :account_management_pages, path: '' do
        root to: 'users#new', as: :registration
        resources :users, except: %w[index], path_names: { new: 'register' } do
          scope module: :users do 
            resources :user_profiles
          end
        end
      end
    end
    

    ...and then rails routes, you get (amongst other things):

                                             Prefix Verb   URI Pattern                                                Controller#Action
              account_management_pages_registration GET    /                                                          account_management_pages/users#new
        account_management_pages_user_user_profiles GET    /users/:user_id/user_profiles(.:format)                    account_management_pages/users/user_profiles#index
                                                    POST   /users/:user_id/user_profiles(.:format)                    account_management_pages/users/user_profiles#create
     new_account_management_pages_user_user_profile GET    /users/:user_id/user_profiles/register(.:format)           account_management_pages/users/user_profiles#new
    edit_account_management_pages_user_user_profile GET    /users/:user_id/user_profiles/:id/edit(.:format)           account_management_pages/users/user_profiles#edit
         account_management_pages_user_user_profile GET    /users/:user_id/user_profiles/:id(.:format)                account_management_pages/users/user_profiles#show
                                                    PATCH  /users/:user_id/user_profiles/:id(.:format)                account_management_pages/users/user_profiles#update
                                                    PUT    /users/:user_id/user_profiles/:id(.:format)                account_management_pages/users/user_profiles#update
                                                    DELETE /users/:user_id/user_profiles/:id(.:format)                account_management_pages/users/user_profiles#destroy
                     account_management_pages_users POST   /users(.:format)                                           account_management_pages/users#create
                  new_account_management_pages_user GET    /users/register(.:format)                                  account_management_pages/users#new
                 edit_account_management_pages_user GET    /users/:id/edit(.:format)                                  account_management_pages/users#edit
                      account_management_pages_user GET    /users/:id(.:format)                                       account_management_pages/users#show
                                                    PATCH  /users/:id(.:format)                                       account_management_pages/users#update
                                                    PUT    /users/:id(.:format)                                       account_management_pages/users#update
                                                    DELETE /users/:id(.:format)                                       account_management_pages/users#destroy
    

    ...and user_profiles will be nested under users. And you should be able to use:

    module AccountManagementPages
      module Users
        class UserProfilesController < ApplicationController
    
          ...
    
        end
      end
    end