RESTomatic helps Rails developers organize nested resources with automatic namespacing. Each nested resource gets its own controller module, making your app cleaner and easier to maintain.
Unlike Rails shallow routes that send everything to one controller, RESTomatic enforces proper separation: Blogs::PostsController, Users::PostsController, etc. keeping your controllers organized and your codebase more maintainable.
Add to your Rails application Gemfile:
bundleadd"restomatic"That's it! The routing helpers are automatically available in your config/routes.rb file.
Then start using more RESTful routes in config/routes.rb:
resources:blogsdonest:postsdo# Blogs::PostsControllercollectiondoget:search# Blogs::PostsController#searchendendendresources:postsdocreate:comments# Posts::CommentsController#new, #createendRails provides shallow routes and nested resources, but the syntax becomes verbose and repetitive, especially when you want to properly namespace controllers.
resources:blogsdoscopemodule: :blogsdoresources:posts,only: %i[indexnewcreate]docollectiondoget:searchendendendendresources:postsdoscopemodule: :postsdoresources:comments,only: %i[newcreate]endendresources:blogsdonest:postsdo# Blogs::PostsControllercollectiondoget:search# Blogs::PostsController#searchendendendresources:postsdocreate:comments# Posts::CommentsController#new, #createendMuch cleaner! The nest helper automatically:
- Scopes to the parent resource's module (e.g.,
Blogs::PostsController) - Sets sensible defaults for which actions are included
- Reduces boilerplate while maintaining Rails conventions
Nest resources under a parent with automatic module scoping and sensible action defaults.
resources:blogsdonest:posts# Creates index, new, create actions under Blogs::PostsControllernest:post# Singular: creates new, create actions (no edit/update/destroy)endOptions:
except:- Exclude specific actions (overrides defaults)- Singular resources default to excluding:
[:edit, :update, :destroy] - Plural resources default to excluding:
[:show, :edit, :update, :destroy]
Module-only nesting:
resources:postsdonestdo# Routes defined here will be scoped to Posts module# without creating a nested resourceget:analyticsendendDefine routes for creating a resource (new + create actions only).
resources:postsdocreate:comments# Only new and create actionsendcreate:session# Works with both singular and plural formsDefine routes for editing a resource (edit + update actions only).
resources:postsdoedit:metadata# Only edit and update actionsendDefine routes for showing a resource (show action only).
resources:postsdoshow:preview# Only show actionendDefine routes for destroying a resource (destroy action only).
resources:postsdodestroy:attachment# Only destroy actionendDefine routes for listing resources (index action only).
resources:usersdolist:posts# Only index actionendRails.application.routes.drawdoroot"home#index"# Public blog routesresources:blogs,only: [:index,:show]dolist:posts# Blogs::PostsController#indexend# Admin area with nested resourcesnamespace:admindoresources:blogsdonest:postsdo# Admin::Blogs::PostsControllercreate:comments# Admin::Blogs::Posts::CommentsController#new, #createcollectiondoget:scheduled# Admin::Blogs::PostsController#scheduledpost:bulk_publish# Admin::Blogs::PostsController#bulk_publishendendendresources:postsdoedit:seo# Admin::Posts::SeosController#edit, #updateshow:preview# Admin::Posts::PreviewsController#showdestroy:featured_image# Admin::Posts::FeaturedImagesController#destroyendend# User account managementresource:accountdonestdoedit:profile# Accounts::ProfilesController#edit, #updateedit:password# Accounts::PasswordsController#edit, #updateshow:billing# Accounts::BillingsController#showendendendRESTomatic extends ActionDispatch::Routing::Mapper to add these helper methods. The helpers automatically:
- Detect singular vs. plural resource names
- Apply appropriate module scoping
- Set sensible action defaults based on the helper used
- Work seamlessly with existing Rails routing features
Rails routing is powerful but can become verbose when building properly organized applications with shallow routes and namespaced controllers. RESTomatic embraces Rails conventions while reducing boilerplate, making your routes file more readable and maintainable.
- Rails 7.0+
- Ruby 3.0+
- Fork the repository
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
The gem is available as open source under the terms of the MIT License.