instance method namespace

Ruby on Rails 8.1.2

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

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

name req
deprecated_options opt = nil
as key = DEFAULT
path key = DEFAULT
shallow_path key = DEFAULT
shallow_prefix key = DEFAULT
options keyrest
block block
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

Type at least 2 characters to search.

↑↓ navigate · open · esc close