instance method
namespace
Ruby on Rails 8.1.2
Since v3.0.20Signature
namespace(name, deprecated_options = nil, as: DEFAULT, path: DEFAULT, shallow_path: DEFAULT, shallow_prefix: DEFAULT, **options, &block)
Scopes routes to a specific namespace. For example:
namespace :admin do resources :posts end
This generates the following routes:
admin_posts GET /admin/posts(.:format) admin/posts#index
admin_posts POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
admin_post PATCH/PUT /admin/posts/:id(.:format) admin/posts#update
admin_post DELETE /admin/posts/:id(.:format) admin/posts#destroy
Options
The :path, :as, :module, :shallow_path, and :shallow_prefix options all default to the name of the namespace.
For options, see Base#match. For :shallow_path option, see Resources#resources.
# accessible through /sekret/posts rather than /admin/posts namespace :admin, path: "sekret" do resources :posts end # maps to Sekret::PostsController rather than Admin::PostsController namespace :admin, module: "sekret" do resources :posts end # generates sekret_posts_path rather than admin_posts_path namespace :admin, as: "sekret" do resources :posts end
Parameters
-
namereq -
deprecated_optionsopt = nil -
askey = DEFAULT -
pathkey = DEFAULT -
shallow_pathkey = DEFAULT -
shallow_prefixkey = DEFAULT -
optionskeyrest -
blockblock
Source
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1097
def namespace(name, deprecated_options = nil, as: DEFAULT, path: DEFAULT, shallow_path: DEFAULT, shallow_prefix: DEFAULT, **options, &block)
if deprecated_options.is_a?(Hash)
as = assign_deprecated_option(deprecated_options, :as, :namespace) if deprecated_options.key?(:as)
path ||= assign_deprecated_option(deprecated_options, :path, :namespace) if deprecated_options.key?(:path)
shallow_path ||= assign_deprecated_option(deprecated_options, :shallow_path, :namespace) if deprecated_options.key?(:shallow_path)
shallow_prefix ||= assign_deprecated_option(deprecated_options, :shallow_prefix, :namespace) if deprecated_options.key?(:shallow_prefix)
assign_deprecated_options(deprecated_options, options, :namespace)
end
name = name.to_s
options[:module] ||= name
as = name if as == DEFAULT
path = name if path == DEFAULT
shallow_path = path if shallow_path == DEFAULT
shallow_prefix = as if shallow_prefix == DEFAULT
path_scope(path) do
scope(**options, as:, shallow_path:, shallow_prefix:, &block)
end
end
Defined in actionpack/lib/action_dispatch/routing/mapper.rb line 1097
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionDispatch::Routing::Mapper::Scoping