跳至内容 跳至搜索

资源路由允许您快速声明给定资源控制器的所有常见路由。 不必为您的indexshowneweditcreateupdatedestroy操作声明单独的路由,一个资源路由在一行代码中声明它们。

resources :photos

有时,您有一个资源,客户端始终在不引用 ID 的情况下查找它。 一个常见的例子,/profile 始终显示当前登录用户的个人资料。 在这种情况下,您可以使用单个资源将 /profile(而不是 /profile/:id)映射到 show 操作。

resource :profile

资源在逻辑上是其他资源的子资源的情况很常见。

resources :magazines do
  resources :ads
end

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

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

默认情况下,:id 参数不接受点。 如果您需要在 :id 参数中使用点,请添加一个约束来覆盖此限制,例如

resources :articles, id: /[^\/]+/

这允许除斜杠之外的任何字符作为您 :id 的一部分。

方法
A
C
D
M
N
R
S
W

常量

CANONICAL_ACTIONS = %w(index create new show update destroy)
 
RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except, :param, :concerns]
 
VALID_ON_OPTIONS = [:new, :collection, :member]
 

CANONICAL_ACTIONS 包含所有不需要前缀或附加路径的操作,因为它们恰好适合其作用域级别。

实例公共方法

collection(&block)

向集合添加路由

resources :photos do
  collection do
    get 'search'
  end
end

这将使 Rails 能够识别诸如 /photos/search(使用 GET)之类的路径,并将路由到 PhotosController 的 search 操作。 它还会创建 search_photos_urlsearch_photos_path 路由助手。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1558
def collection(&block)
  unless resource_scope?
    raise ArgumentError, "can't use collection outside resource(s) scope"
  end

  with_scope_level(:collection) do
    path_scope(parent_resource.collection_scope, &block)
  end
end

draw(name)

使用位于 config/routes 目录内的给定 name 加载另一个路由文件。 在该文件中,您可以使用正常的路由 DSL,但不要用 Rails.application.routes.draw 块将其包围。

# config/routes.rb
Rails.application.routes.draw do
  draw :admin                 # Loads `config/routes/admin.rb`
  draw "third_party/some_gem" # Loads `config/routes/third_party/some_gem.rb`
end

# config/routes/admin.rb
namespace :admin do
  resources :accounts
end

# config/routes/third_party/some_gem.rb
mount SomeGem::Engine, at: "/some_gem"

警告:谨慎使用此功能。 拥有多个路由文件会对可发现性和可读性产生负面影响。 对于大多数应用程序(即使是包含数百条路由的应用程序),对开发人员来说,拥有一个路由文件更容易。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1667
def draw(name)
  path = @draw_paths.find do |_path|
    File.exist? "#{_path}/#{name}.rb"
  end

  unless path
    msg  = "Your router tried to #draw the external file #{name}.rb,\n" \
           "but the file was not found in:\n\n"
    msg += @draw_paths.map { |_path| " * #{_path}" }.join("\n")
    raise ArgumentError, msg
  end

  route_path = "#{path}/#{name}.rb"
  instance_eval(File.read(route_path), route_path.to_s)
end

match(path, *rest, &block)

将 URL 模式匹配到一个或多个路由。 有关更多信息,请参阅 match

match 'path', to: 'controller#action', via: :post
match 'path', 'otherpath', on: :member, via: :get
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1688
def match(path, *rest, &block)
  if rest.empty? && Hash === path
    options  = path
    path, to = options.find { |name, _value| name.is_a?(String) }

    raise ArgumentError, "Route path not specified" if path.nil?

    case to
    when Symbol
      options[:action] = to
    when String
      if to.include?("#")
        options[:to] = to
      else
        options[:controller] = to
      end
    else
      options[:to] = to
    end

    options.delete(path)
    paths = [path]
  else
    options = rest.pop || {}
    paths = [path] + rest
  end

  if options.key?(:defaults)
    defaults(options.delete(:defaults)) { map_match(paths, options, &block) }
  else
    map_match(paths, options, &block)
  end
end

member(&block)

要添加成员路由,请在资源块中添加一个成员块

resources :photos do
  member do
    get 'preview'
  end
end

这将识别使用 GET 的 /photos/1/preview,并将路由到 PhotosController 的 preview 操作。 它还会创建 preview_photo_urlpreview_photo_path 助手。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1579
def member(&block)
  unless resource_scope?
    raise ArgumentError, "can't use member outside resource(s) scope"
  end

  with_scope_level(:member) do
    if shallow?
      shallow_scope {
        path_scope(parent_resource.member_scope, &block)
      }
    else
      path_scope(parent_resource.member_scope, &block)
    end
  end
end

namespace(path, options = {})

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1626
def namespace(path, options = {})
  if resource_scope?
    nested { super }
  else
    super
  end
end

nested(&block)

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1605
def nested(&block)
  unless resource_scope?
    raise ArgumentError, "can't use nested outside resource(s) scope"
  end

  with_scope_level(:nested) do
    if shallow? && shallow_nesting_depth >= 1
      shallow_scope do
        path_scope(parent_resource.nested_scope) do
          scope(nested_options, &block)
        end
      end
    else
      path_scope(parent_resource.nested_scope) do
        scope(nested_options, &block)
      end
    end
  end
end

new(&block)

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1595
def new(&block)
  unless resource_scope?
    raise ArgumentError, "can't use new outside resource(s) scope"
  end

  with_scope_level(:new) do
    path_scope(parent_resource.new_scope(action_path(:new)), &block)
  end
end

resource(*resources, &block)

有时,您有一个资源,客户端始终在不引用 ID 的情况下查找它。 一个常见的例子,/profile 始终显示当前登录用户的个人资料。 在这种情况下,您可以使用单个资源将 /profile(而不是 /profile/:id)映射到 show 操作。

resource :profile

这将在您的应用程序中创建六个不同的路由,所有路由都映射到 Profiles 控制器(请注意,控制器以复数命名)。

GET       /profile/new
GET       /profile
GET       /profile/edit
PATCH/PUT /profile
DELETE    /profile
POST      /profile

如果希望模型实例通过记录标识与该资源一起使用(例如,在 form_withredirect_to 中),您需要调用 resolve

resource :profile
resolve('Profile') { [:profile] }

# Enables this to work with singular routes:
form_with(model: @profile) {}

选项

接受与 resources 相同的选项。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1347
def resource(*resources, &block)
  options = resources.extract_options!.dup

  if apply_common_behavior_for(:resource, resources, options, &block)
    return self
  end

  with_scope_level(:resource) do
    options = apply_action_options :resource, options
    resource_scope(SingletonResource.new(resources.pop, api_only?, @scope[:shallow], options)) do
      yield if block_given?

      concerns(options[:concerns]) if options[:concerns]

      new do
        get :new
      end if parent_resource.actions.include?(:new)

      set_member_mappings_for_resource

      collection do
        post :create
      end if parent_resource.actions.include?(:create)
    end
  end

  self
end

resources(*resources, &block)

Rails 中,资源路由提供了 HTTP 动词和 URL 与控制器操作之间的映射。 按照惯例,每个操作还映射到数据库中的特定 CRUD 操作。 路由文件中的单个条目,例如

resources :photos

在您的应用程序中创建七个不同的路由,所有路由都映射到 Photos 控制器。

GET       /photos
GET       /photos/new
POST      /photos
GET       /photos/:id
GET       /photos/:id/edit
PATCH/PUT /photos/:id
DELETE    /photos/:id

Resources 也可以通过使用此块语法无限嵌套。

resources :photos do
  resources :comments
end

这会生成以下注释路由

GET       /photos/:photo_id/comments
GET       /photos/:photo_id/comments/new
POST      /photos/:photo_id/comments
GET       /photos/:photo_id/comments/:id
GET       /photos/:photo_id/comments/:id/edit
PATCH/PUT /photos/:photo_id/comments/:id
DELETE    /photos/:photo_id/comments/:id

选项

接受与 match 相同的选项,以及

:path_names

允许您更改 editnew 操作的段组件。 未指定的动作不会更改。

    resources :posts, path_names: { new: "brand_new" }

The above example will now change /posts/new to /posts/brand_new.
:path

允许您更改资源的路径前缀。

    resources :posts, path: 'postings'

The resource and all segments will now route to /postings instead of
/posts.
:only

仅为给定操作生成路由。

    resources :cows, only: :show
    resources :cows, only: [:show, :index]
:except

生成除给定操作之外的所有路由。

    resources :cows, except: :show
    resources :cows, except: [:show, :index]
:shallow

为嵌套资源生成浅层路由。 当放置在父资源上时,会为所有嵌套资源生成浅层路由。

    resources :posts, shallow: true do
      resources :comments
    end

Is the same as:

    resources :posts do
      resources :comments, except: [:show, :edit, :update, :destroy]
    end
    resources :comments, only: [:show, :edit, :update, :destroy]

This allows URLs for resources that otherwise would be deeply nested such
as a comment on a blog post like `/posts/a-long-permalink/comments/1234`
to be shortened to just `/comments/1234`.

Set `shallow: false` on a child resource to ignore a parent's shallow
parameter.
:shallow_path

使用指定路径为嵌套的浅层路由添加前缀。

    scope shallow_path: "sekret" do
      resources :posts do
        resources :comments, shallow: true
      end
    end

The `comments` resource here will have the following routes generated for
it:

    post_comments    GET       /posts/:post_id/comments(.:format)
    post_comments    POST      /posts/:post_id/comments(.:format)
    new_post_comment GET       /posts/:post_id/comments/new(.:format)
    edit_comment     GET       /sekret/comments/:id/edit(.:format)
    comment          GET       /sekret/comments/:id(.:format)
    comment          PATCH/PUT /sekret/comments/:id(.:format)
    comment          DELETE    /sekret/comments/:id(.:format)
:shallow_prefix

使用指定前缀为嵌套的浅层路由名称添加前缀。

    scope shallow_prefix: "sekret" do
      resources :posts do
        resources :comments, shallow: true
      end
    end

The `comments` resource here will have the following routes generated for
it:

    post_comments           GET       /posts/:post_id/comments(.:format)
    post_comments           POST      /posts/:post_id/comments(.:format)
    new_post_comment        GET       /posts/:post_id/comments/new(.:format)
    edit_sekret_comment     GET       /comments/:id/edit(.:format)
    sekret_comment          GET       /comments/:id(.:format)
    sekret_comment          PATCH/PUT /comments/:id(.:format)
    sekret_comment          DELETE    /comments/:id(.:format)
:format

允许您指定可选 format 段的默认值,或通过提供 false 来禁用它。

:param

允许您覆盖 URL 中 :id 的默认参数名称。

示例

# routes call +Admin::PostsController+
resources :posts, module: "admin"

# resource actions are at /admin/posts.
resources :posts, path: "admin/posts"
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1517
def resources(*resources, &block)
  options = resources.extract_options!.dup

  if apply_common_behavior_for(:resources, resources, options, &block)
    return self
  end

  with_scope_level(:resources) do
    options = apply_action_options :resources, options
    resource_scope(Resource.new(resources.pop, api_only?, @scope[:shallow], options)) do
      yield if block_given?

      concerns(options[:concerns]) if options[:concerns]

      collection do
        get  :index if parent_resource.actions.include?(:index)
        post :create if parent_resource.actions.include?(:create)
      end

      new do
        get :new
      end if parent_resource.actions.include?(:new)

      set_member_mappings_for_resource
    end
  end

  self
end

resources_path_names(options)

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1314
def resources_path_names(options)
  @scope[:path_names].merge!(options)
end

root(path, options = {})

您可以使用 root 方法指定 Rails 应该将“/”路由到什么。

root to: 'pages#main'

有关选项,请参阅 match,因为 root 在内部使用它。

您也可以传递一个字符串,它会扩展

root 'pages#main'

您应该将 root 路由放在 config/routes.rb 的顶部,因为这意味着它将首先被匹配。 由于这是大多数 Rails 应用程序中最流行的路由,因此这样做是有益的。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1735
def root(path, options = {})
  if path.is_a?(String)
    options[:to] = path
  elsif path.is_a?(Hash) && options.empty?
    options = path
  else
    raise ArgumentError, "must be called with a path and/or options"
  end

  if @scope.resources?
    with_scope_level(:root) do
      path_scope(parent_resource.path) do
        match_root_route(options)
      end
    end
  else
    match_root_route(options)
  end
end

shallow()

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1634
def shallow
  @scope = @scope.new(shallow: true)
  yield
ensure
  @scope = @scope.parent
end

shallow?()

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1641
def shallow?
  !parent_resource.singleton? && @scope[:shallow]
end

实例私有方法

api_only?()

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1950
def api_only? # :doc:
  @set.api_only?
end

set_member_mappings_for_resource()

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1938
def set_member_mappings_for_resource # :doc:
  member do
    get :edit if parent_resource.actions.include?(:edit)
    get :show if parent_resource.actions.include?(:show)
    if parent_resource.actions.include?(:update)
      patch :update
      put   :update
    end
    delete :destroy if parent_resource.actions.include?(:destroy)
  end
end

with_scope_level(kind)

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1834
def with_scope_level(kind) # :doc:
  @scope = @scope.new_level(kind)
  yield
ensure
  @scope = @scope.parent
end