Search code examples
ruby-on-railsruby-on-rails-4namespacescode-organizationsubdirectory

How to organize controller directory in rails 4 without messing up routes


First off... I love keeping things organized. As such, it's starting to bother me that the list of controllers in my app just keeps growing in one large directory.

Ideally, I could construct a list of subdirectories and organize my controllers. You can do this with namespaces, but then the subdirectory shows up within the url, and I really don't want this to happen.

Does anyone have a different strategy to keep their controllers, helpers, models, and views organized?


Solution

  • Ideally, I could construct a list of subdirectories and organize my controllers. You can do this with namespaces, but then the subdirectory shows up within the url, and I really don't want this to happen.

    You can scope the routes against a specific namespace. Read Controller Namespaces and Routing

    scope module: 'admin' do
      resources :posts, :comments
    end
    

    Will generate routes at /posts while the controller is at Admin::PostsController found in app/controllers/admin/posts_controller.rb.