資源路由可讓您快速宣告特定多資源控制器中所有的共用路由。多資源路由宣告於單一行程式碼中,而非宣告個別的路由給您的 `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
識別 GET 類型的路徑,例如 `/photos/search`,並路由到 PhotosController 的 `search` 動作。其也會建立 `search_photos_url` 和 `search_photos_path` 路由幫手。
來源:顯示 | 在 GitHub 上
# 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"
警告:請小心使用此功能。擁有多個路由檔案可能會對可發現性和易讀性造成負面影響。對大多數應用程式而言,即使擁有數百個路由,開發人員只需要單一路由檔案就較為容易。
來源:顯示 | 在 GitHub 上
# 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
出處:顯示 | 於 GitHub 上
# 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_photo_url
和 preview_photo_path
輔助程式。
出處:顯示 | 於 GitHub 上
# 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 = {}) 連結
出處:顯示 | 於 GitHub 上
# 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) 連結
出處:顯示 | 於 GitHub 上
# 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) 連結
出處:顯示 | 於 GitHub 上
# 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) 繫結到顯示動作
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) {}
選項
使用與 資源 相同選項
出處:顯示 | 於 GitHub 上
# 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 :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" }
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(選項) 連結
root(路徑, 選項 = {}) 連結
可透過 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() 連結
shallow?() 連結
執行個體私有方法
api_only?() 連結
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