跳到內容 跳到搜尋
Method
D
H
M
W

執行個體的公用方法

default_url_options(options)

default_url_options=(options)

亦有別名:default_url_options
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 640
def default_url_options=(options)
  @set.default_url_options = options
end

has_named_route?(name)

查詢是否有定義下列已命名路由。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 652
def has_named_route?(name)
  @set.named_routes.key?(name)
end

match(path, options = nil)

將 URL 模式對應到一個以上路由。

您不應於路由器中使用 match 方法而未指定 HTTP 方法。

若您想同時對 GET 及 POST 顯示您的動作,請使用

# sets :controller, :action, and :id in params
match ':controller/:action/:id', via: [:get, :post]

注意,:controller:action:id 被解譯成 URL 查詢參數,因此可用於動作中的 params

若您想對 GET 顯示您的動作,請在路由器中使用 get

取代

match ":controller/:action/:id"

這麼做

get ":controller/:action/:id"

這當中兩個符號很特別,:controller 對應到 controlador,而 :action 對應到 controlador 的動作。模式也能將萬用字元區段 (globs) 對應到 params

get 'songs/*category/:title', to: 'songs#show'

# 'songs/rock/classic/stairway-to-heaven' sets
#  params[:category] = 'rock/classic'
#  params[:title] = 'stairway-to-heaven'

為對應一個萬用字元參數,必須指定名稱給它。如果沒有可將萬用字元參數附加的變數名稱,則無法剖析路線。\

當模式指向內部路由時,應在選項或雜湊簡寫中設定路由的 :action:controller。範例

match 'photos/:id', to: 'photos#show', via: :get
match 'photos/:id', controller: 'photos', action: 'show', via: :get

一個模式亦可指向 Rack 端點,亦即任何會回應 call 的東西

match 'photos/:id', to: -> (hash) { [200, {}, ["Coming soon"]] }, via: :get
match 'photos/:id', to: PhotoRackApp, via: :get
# Yes, controller actions are just rack endpoints
match 'photos/:id', to: PhotosController.action(:show), via: :get

由於使用單一動作請求各種 HTTP 動詞有安全性問題,您必須在 via 選項中指定動作,或是使用 HttpHelpers 之一取代 match

選項

此處未顯示的任何選項都當成 params 跟 URL 一起傳遞。

:controller

路由的 controlador。

:action

路由的動作。

:param

覆寫預設資源識別碼 :id (用於產生路由的動態區段名稱)。您可以使用 params[<:param>] 從控制器取用該區段。於您的路由器中

    resources :users, param: :name

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

    GET       /users(.:format)
    POST      /users(.:format)
    GET       /users/new(.:format)
    GET       /users/:name/edit(.:format)
    GET       /users/:name(.:format)
    PATCH/PUT /users/:name(.:format)
    DELETE    /users/:name(.:format)

You can override `ActiveRecord::Base#to_param` of a related model to
construct a URL:

    class User < ActiveRecord::Base
      def to_param
        name
      end
    end

    user = User.find_by(name: 'Phusion')
    user_path(user)  # => "/users/Phusion"
:path

路由的途徑字首。

:module

:controller 的命名空間。

    match 'path', to: 'c#a', module: 'sekret', controller: 'posts', via: :get
    # => Sekret::PostsController

See `Scoping#namespace` for its scope equivalent.
:as

用於產生路由 helper 的名稱。

:via

路由允許的 HTTP 動作。

    match 'path', to: 'c#a', via: :get
    match 'path', to: 'c#a', via: [:get, :post]
    match 'path', to: 'c#a', via: :all
:to

指向一個 Rack 端點。可以是一個對 call 有反應的物件,或是表示控制器的動作的字串。

    match 'path', to: 'controller#action', via: :get
    match 'path', to: -> (env) { [200, {}, ["Success!"]] }, via: :get
    match 'path', to: RackApp, via: :get
:on

將路由包裝在特定 RESTful 內容中的簡寫。有效值有 :member:collection:new。只在 resource(s) 區塊中使用。例如

    resource :bar do
      match 'foo', to: 'c#a', on: :member, via: [:get, :post]
    end

Is equivalent to:

    resource :bar do
      member do
        match 'foo', to: 'c#a', via: [:get, :post]
      end
    end
:constraints

用一個正規表示式的雜湊或一個會對 matches? 有反應的物件來限制參數。此外,除了路徑以外的限制,也可以使用任何會對 === 有反應的物件來指定(例如 StringArrayRange 等)。

    match 'path/:id', constraints: { id: /[A-Z]\d{5}/ }, via: :get

    match 'json_only', constraints: { format: 'json' }, via: :get

    class PermitList
      def matches?(request) request.remote_ip == '1.2.3.4' end
    end
    match 'path', to: 'c#a', constraints: PermitList.new, via: :get

See `Scoping#constraints` for more examples with its scope equivalent.
:defaults

設定參數的預設值

    # Sets params[:format] to 'jpg' by default
    match 'path', to: 'c#a', defaults: { format: 'jpg' }, via: :get

See `Scoping#defaults` for its scope equivalent.
:anchor

用於固定一個 match 型式的布林值。預設為 true。設定為 false 時,該型式會符合任何以給定路徑為字首的要求。

    # Matches any request starting with 'path'
    match 'path', to: 'c#a', anchor: false, via: :get
:format

讓你可以指定一個可選 format 段落的預設值,或提供 false 來關閉這個功能。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 592
def match(path, options = nil)
end

mount(app, options = nil)

將基於 Rack 的應用程式掛載進去,以便在應用程式中使用。

mount SomeRackApp, at: "some_route"

關於選項,請參閱 match,因為 mount 在內部使用它。

所有掛載的應用程式都會提供路由 helper 來存取它們。這些 helper 會根據所指定類別而命名,因此在上述範例中,helper 要不就是 some_rack_app_path 就是 some_rack_app_url。若要自訂這個 helper 的名稱,可以使用 :as 選項

mount(SomeRackApp, at: "some_route", as: "exciting")

這會產生 exciting_pathexciting_url helper,可用於導覽到這個掛載的應用程式。

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 610
        def mount(app, options = nil)
          if options
            path = options.delete(:at)
          elsif Hash === app
            options = app
            app, path = options.find { |k, _| k.respond_to?(:call) }
            options.delete(app) if app
          end

          raise ArgumentError, "A rack application must be specified" unless app.respond_to?(:call)
          raise ArgumentError, <<~MSG unless path
            Must be called with mount point

              mount SomeRackApp, at: "some_route"
              or
              mount(SomeRackApp => "some_route")
          MSG

          rails_app = rails_app? app
          options[:as] ||= app_name(app, rails_app)

          target_as       = name_for_action(options[:as], path)
          options[:via] ||= :all

          match(path, { to: app, anchor: false, format: false }.merge(options))

          define_generate_prefix(app, target_as) if rails_app
          self
        end

with_default_scope(scope, &block)

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 645
def with_default_scope(scope, &block)
  scope(scope) do
    instance_exec(&block)
  end
end