0

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.

2
  • 1
    What does "rake routes | grep account_management_pages" say? Nested routes don't require a nester folder structure. A namespace usually does but that can be managed by specifying a path, a controller, etc. Commented Sep 25, 2020 at 5:27
  • @razvans it was the controller.. lol.. i do not know how i overlooked naming the controller in the routes file.. lol.. thanks! Ive been staring at that for far too long! Commented Sep 25, 2020 at 6:06

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.