资源路由允许你快速声明给定资源控制器的所有常见路由。无需为你的 index
、show
、new
、edit
、create
、update
和 destroy
操作声明单独的路由,资源路由在单行代码中声明这些操作
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] |
|
实例公共方法
collection(&block) 链接
向集合添加路由
resources :photos do
collection do
get 'search'
end
end
这将使 Rails 能够识别诸如 /photos/search
的路径,并使用 GET 路由到 PhotosController
的 search 操作。它还将创建 search_photos_url
和 search_photos_path
路由助手。
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1511 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"
注意:谨慎使用此功能。拥有多个路由文件可能会对可发现性和可读性产生负面影响。对于大多数应用程序(即使是那些有数百个路由的应用程序),开发人员拥有单个路由文件会更容易。
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1621 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' => 'controller#action', via: :patch
match 'path', to: 'controller#action', via: :post
match 'path', 'otherpath', on: :member, via: :get
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1643 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_url
和 preview_photo_path
帮助器。
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1532 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 = {}) 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1579 def namespace(path, options = {}) if resource_scope? nested { super } else super end end
nested(&block) 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1558 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) 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1548 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_with
或 redirect_to
中)使用此资源,则需要调用 resolve
resource :profile
resolve('Profile') { [:profile] }
# Enables this to work with singular routes:
form_with(model: @profile) {}
选项
采用与 resources 相同的选项
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1303 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 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
-
允许您更改
edit
和new
操作的段组件。未指定的操作不会更改。resources :posts, path_names: { new: "brand_new" }
上述示例现在会将 /posts/new 更改为 /posts/brand_new。
- :path
-
允许您更改资源的路径前缀。
resources :posts, path: 'postings'
资源和所有片段现在将路由到 /postings 而不是 /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
与以下内容相同
resources :posts do resources :comments, except: [:show, :edit, :update, :destroy] end resources :comments, only: [:show, :edit, :update, :destroy]
这允许 URL 缩短到仅为
/comments/1234
,否则这些 URL 将深度嵌套,例如博客文章上的评论,如/posts/a-long-permalink/comments/1234
。在子资源上设置
shallow: false
以忽略父资源的 shallow 参数。 - :shallow_path
-
为嵌套浅层路由添加指定路径的前缀。
scope shallow_path: "sekret" do resources :posts do resources :comments, shallow: true end end
此处的
comments
资源将为其生成以下路由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
此处的
comments
资源将为其生成以下路由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"
源代码: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1469 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 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) 链接
源代码: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1268 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'
你应将根路由放在 config/routes.rb
的顶部,因为这意味着它将首先匹配。由于这是大多数 Rails 应用程序最常用的路由,因此这样做是有益的。
源代码: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1690 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() 链接
源代码: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1587 def shallow @scope = @scope.new(shallow: true) yield ensure @scope = @scope.parent end
shallow?() 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1594 def shallow? !parent_resource.singleton? && @scope[:shallow] end
实例私有方法
api_only?() 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1890 def api_only? # :doc: @set.api_only? end
set_member_mappings_for_resource() 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1878 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) 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1774 def with_scope_level(kind) # :doc: @scope = @scope.new_level(kind) yield ensure @scope = @scope.parent end