跳至内容 跳至搜索

你可能希望在命名空间下组织控制器组。最常见的是,你可能将一些管理控制器分组在 admin 命名空间下。你将这些控制器放在 app/controllers/admin 目录下,并且可以在路由器中将它们分组在一起

namespace "admin" do
  resources :posts, :comments
end

这将为每个帖子和评论控制器创建许多路由。对于 Admin::PostsController,Rails 将创建

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

如果你想将 /posts(不带前缀 /admin)路由到 Admin::PostsController,可以使用

scope module: "admin" do
  resources :posts
end

或者,对于单个案例

resources :posts, module: "admin"

如果你想将 /admin/posts 路由到 PostsController(不带 Admin:: 模块前缀),可以使用

scope "/admin" do
  resources :posts
end

或者,对于单个案例

resources :posts, path: "/admin/posts"

在每种情况下,命名路由与不使用范围时保持相同。在最后一种情况下,以下路径映射到 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
方法
C
D
N
S

实例公共方法

constraints(constraints = {}, &block)

参数限制

允许你根据一组规则来约束嵌套路由。例如,为了更改路由以允许 id 参数中出现点字符

constraints(id: /\d+\.\d+/) do
  resources :posts
end

现在,诸如 /posts/1 的路由将不再有效,但 /posts/1.1 将有效。id 参数必须与为此示例传递的约束匹配。

您还可以使用此功能来限制其他参数

resources :posts do
  constraints(post_id: /\d+\.\d+/) do
    resources :comments
  end
end

基于 IP 进行限制

路由还可以限制为 IP 或特定范围的 IP 地址

constraints(ip: /192\.168\.\d+\.\d+/) do
  resources :posts
end

任何从 192.168.* 范围连接的用户都将能够看到此资源,而任何在此范围之外连接的用户都将被告知没有此类路由。

动态请求匹配

可以基于特定条件来限制对路由的请求

constraints(-> (req) { /iPhone/.match?(req.env["HTTP_USER_AGENT"]) }) do
  resources :iphones
end

如果对于路由来说过于复杂,您可以将此逻辑移到一个类中。此类必须定义一个 matches? 方法,如果用户应该被允许访问该路由,则返回 true,如果用户不应该访问该路由,则返回 false

class Iphone
  def self.matches?(request)
    /iPhone/.match?(request.env["HTTP_USER_AGENT"])
  end
end

此代码的预期位置是 lib/constraints

然后像这样使用此类

constraints(Iphone) do
  resources :iphones
end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1010
def constraints(constraints = {}, &block)
  scope(constraints: constraints, &block)
end

controller(controller)

将路由范围限定到特定控制器

controller "food" do
  match "bacon", action: :bacon, via: :get
end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 895
def controller(controller)
  @scope = @scope.new(controller: controller)
  yield
ensure
  @scope = @scope.parent
end

defaults(defaults = {})

允许您为路由设置默认参数,例如此参数

defaults id: 'home' do
  match 'scoped_pages/(:id)', to: 'pages#show'
end

使用此参数,此处的 :id 参数将默认为“home”。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1019
def defaults(defaults = {})
  @scope = @scope.new(defaults: merge_defaults_scope(@scope[:defaults], defaults))
  yield
ensure
  @scope = @scope.parent
end

namespace(path, options = {}, &block)

将路由范围限定到特定命名空间。例如

namespace :admin do
  resources :posts
end

生成以下路由

    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

选项

:path:as:module:shallow_path:shallow_prefix 选项均默认为命名空间的名称。

有关选项,请参阅 Base#match。有关 :shallow_path 选项,请参阅 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
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 940
def namespace(path, options = {}, &block)
  path = path.to_s

  defaults = {
    module:         path,
    as:             options.fetch(:as, path),
    shallow_path:   options.fetch(:path, path),
    shallow_prefix: options.fetch(:as, path)
  }

  path_scope(options.delete(:path) { path }) do
    scope(defaults.merge!(options), &block)
  end
end

scope(*args)

将一组路由作用域设为给定的默认选项。

以下路由定义为例

scope path: ":account_id", as: "account" do
  resources :projects
end

这会生成诸如 account_projects_path 之类的帮助器,就像 resources 所做的那样。这里的区别在于生成的路由类似于 /:account_id/projects,而不是 /accounts/:account_id/projects。

选项

采用与 Base#matchResources#resources 相同的选项。

# route /posts (without the prefix /admin) to +Admin::PostsController+
scope module: "admin" do
  resources :posts
end

# prefix the posts resource's requests with '/admin'
scope path: "/admin" do
  resources :posts
end

# prefix the routing helper name: +sekret_posts_path+ instead of +posts_path+
scope as: "sekret" do
  resources :posts
end
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 836
def scope(*args)
  options = args.extract_options!.dup
  scope = {}

  options[:path] = args.flatten.join("/") if args.any?
  options[:constraints] ||= {}

  unless nested_scope?
    options[:shallow_path] ||= options[:path] if options.key?(:path)
    options[:shallow_prefix] ||= options[:as] if options.key?(:as)
  end

  if options[:constraints].is_a?(Hash)
    defaults = options[:constraints].select do |k, v|
      URL_OPTIONS.include?(k) && (v.is_a?(String) || v.is_a?(Integer))
    end

    options[:defaults] = defaults.merge(options[:defaults] || {})
  else
    block, options[:constraints] = options[:constraints], {}
  end

  if options.key?(:only) || options.key?(:except)
    scope[:action_options] = { only: options.delete(:only),
                               except: options.delete(:except) }
  end

  if options.key? :anchor
    raise ArgumentError, "anchor is ignored unless passed to `match`"
  end

  @scope.options.each do |option|
    if option == :blocks
      value = block
    elsif option == :options
      value = options
    else
      value = options.delete(option) { POISON }
    end

    unless POISON == value
      scope[option] = send("merge_#{option}_scope", @scope[option], value)
    end
  end

  @scope = @scope.new scope
  yield
  self
ensure
  @scope = @scope.parent
end