跳到內容 跳到搜尋

資源路由讓您可以快速宣告給定資源控制器所有常見的路由。不用為您的 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_photos_urlsearch_photos_path 路徑輔助工具。

# 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"

注意:請小心使用此功能。擁有多個路徑檔案可能會對可發現性和可讀性產生負面影響。對於大多數應用程式(即使是擁有數百個路徑的應用程式),開發人員擁有單一路徑檔案會比較容易。

# 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
# 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

這將辨識 /photos/1/preview 並使用 GET,並將其導向 PhotosController 的預覽動作。它也會建立 preview_photo_urlpreview_photo_path 輔助工具。

# 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 = {})

# 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)

# 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)

# 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_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 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

允許你變更 editnew 動作的區段元件。未指定的動作不會變更。

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 縮短,例如部落格文章的留言,原本會像 /posts/a-long-permalink/comments/1234 這樣深度巢狀,現在可以縮短為 /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"
# 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(選項)

# 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'

您應該將 root 路由放在 config/routes.rb 的頂端,因為這表示它會先被比對。由於這是大多數 Rails 應用程式最熱門的路由,因此這是有益的。

# 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()

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

shallow?()

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

實例私有方法

api_only?()

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

set_member_mappings_for_resource()

# 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)

# 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