module Scoping
Ruby on Rails 5.2.8.1
Since v3.0.20You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an admin namespace. You would place these controllers under the app/controllers/admin directory, and you can group them together in your router:
namespace "admin" do resources :posts, :comments end
This will create a number of routes for each of the posts and comments controller. For Admin::PostsController, Rails will create:
GET /admin/posts GET /admin/posts/new POST /admin/posts GET /admin/posts/1 GET /admin/posts/1/edit PATCH/PUT /admin/posts/1 DELETE /admin/posts/1
If you want to route /posts (without the prefix /admin) to Admin::PostsController, you could use
scope module: "admin" do resources :posts end
or, for a single case
resources :posts, module: "admin"
If you want to route /admin/posts to PostsController (without the Admin:: module prefix), you could use
scope "/admin" do resources :posts end
or, for a single case
resources :posts, path: "/admin/posts"
In each of these cases, the named routes remain the same as if you did not use scope. In the last case, the following paths map to PostsController:
GET /admin/posts GET /admin/posts/new POST /admin/posts GET /admin/posts/1 GET /admin/posts/1/edit PATCH/PUT /admin/posts/1 DELETE /admin/posts/1
Constants
Methods (defined here)
- # constraints
- # controller
- # defaults
- # namespace
- # scope
Private methods
(16)
Implementation detail — not part of the public API.
- # merge_action_scope
- # merge_as_scope
- # merge_blocks_scope
- # merge_constraints_scope
- # merge_controller_scope
- # merge_defaults_scope
- # merge_format_scope
- # merge_module_scope
- # merge_options_scope
- # merge_path_names_scope
- # merge_path_scope
- # merge_shallow_path_scope
- # merge_shallow_prefix_scope
- # merge_shallow_scope
- # merge_to_scope
- # merge_via_scope