跳至內容 跳至搜尋

動作控制器 Metal

ActionController::Metal 是最簡單的控制器,提供有效的 Rack 介面,但不包含 ActionController::Base 提供的額外功能。

一個 Metal 控制器範例可能如下所示

class HelloController < ActionController::Metal
  def index
    self.response_body = "Hello World!"
  end
end

然後,要將請求路由到您的 Metal 控制器,您需要在 config/routes.rb 中添加類似以下的內容

get 'hello', to: HelloController.action(:index)

::action 方法會傳回一個有效的 Rack 應用程式,供 Rails 路由器分派。

渲染 輔助方法

預設情況下,ActionController::Metal 不提供渲染視圖、局部視圖或其他響應的工具,除了一些低階的設定器,例如 response_body=content_type=status=。要添加您在一般控制器中習慣使用的渲染輔助方法,您可以執行以下操作

class HelloController < ActionController::Metal
  include AbstractController::Rendering
  include ActionView::Layouts
  append_view_path "#{Rails.root}/app/views"

  def index
    render "hello/index"
  end
end

重新導向 輔助方法

要將重新導向輔助方法添加到您的 Metal 控制器,請執行以下操作

class HelloController < ActionController::Metal
  include ActionController::Redirecting
  include Rails.application.routes.url_helpers

  def index
    redirect_to root_url
  end
end

其他 輔助方法

您可以參考 ActionController::Base 中包含的模組,以查看您可以引入 Metal 控制器的其他功能。

方法
A
C
D
H
L
M
N
P
R
S
U

屬性

[R] request(請求)

目前請求的 ActionDispatch::Request 實例。

[R] response(響應)

目前響應的 ActionDispatch::Response 實例。

類別公開方法

action(name)

傳回指定動作名稱的 Rack 端點。

# File actionpack/lib/action_controller/metal.rb, line 315
def self.action(name)
  app = lambda { |env|
    req = ActionDispatch::Request.new(env)
    res = make_response! req
    new.dispatch(name, req, res)
  }

  if middleware_stack.any?
    middleware_stack.build(name, app)
  else
    app
  end
end

controller_name()

傳回控制器名稱的最後一部分,使用底線分隔,不包含結尾的 Controller。例如,PostsController 傳回 posts。命名空間會被省略,因此 Admin::PostsController 也會傳回 posts

傳回值

  • 字串

# File actionpack/lib/action_controller/metal.rb, line 130
def self.controller_name
  @controller_name ||= (name.demodulize.delete_suffix("Controller").underscore unless anonymous?)
end

dispatch(name, req, res)

直接分派到控制器。實例化控制器,然後執行名稱為 name 的動作。

# File actionpack/lib/action_controller/metal.rb, line 331
def self.dispatch(name, req, res)
  if middleware_stack.any?
    middleware_stack.build(name) { |env| new.dispatch(name, req, res) }.call req.env
  else
    new.dispatch(name, req, res)
  end
end

make_response!(request)

# File actionpack/lib/action_controller/metal.rb, line 134
def self.make_response!(request)
  ActionDispatch::Response.new.tap do |res|
    res.request = request
  end
end

middleware()

此控制器使用的中介軟體堆疊。

預設情況下,使用 ActionDispatch::MiddlewareStack 的變體,允許以下語法

class PostsController < ApplicationController
  use AuthenticationMiddleware, except: [:index, :show]
end

在指南中閱讀更多關於 [Rails 中介軟體堆疊] (guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack) 的資訊。

# File actionpack/lib/action_controller/metal.rb, line 310
def self.middleware
  middleware_stack
end

new()

# File actionpack/lib/action_controller/metal.rb, line 210
def initialize
  @_request = nil
  @_response = nil
  @_response_body = nil
  @_routes = nil
  @_params = nil
  super
end

use(...)

將指定的 Rack 中介軟體及其參數推送到中介軟體堆疊的底部。

# File actionpack/lib/action_controller/metal.rb, line 293
def use(...)
  middleware_stack.use(...)
end

實例公開方法

content_type

# File actionpack/lib/action_controller/metal.rb, line 204
delegate :content_type, to: "@_response"

content_type=

# File actionpack/lib/action_controller/metal.rb, line 192
delegate :content_type=, to: "@_response"

controller_name()

委派給類別的 ::controller_name

# File actionpack/lib/action_controller/metal.rb, line 156
def controller_name
  self.class.controller_name
end

headers

# File actionpack/lib/action_controller/metal.rb, line 180
delegate :headers, to: "@_response"

location

# File actionpack/lib/action_controller/metal.rb, line 200
delegate :location, to: "@_response"

location=

# File actionpack/lib/action_controller/metal.rb, line 188
delegate :location=, to: "@_response"

media_type

# File actionpack/lib/action_controller/metal.rb, line 208
delegate :media_type, to: "@_response"

params()

# File actionpack/lib/action_controller/metal.rb, line 219
def params
  @_params ||= request.parameters
end

params=(val)

# File actionpack/lib/action_controller/metal.rb, line 223
def params=(val)
  @_params = val
end

performed?()

測試是否已執行渲染或重新導向。

# File actionpack/lib/action_controller/metal.rb, line 245
def performed?
  response_body || response.committed?
end

reset_session()

# File actionpack/lib/action_controller/metal.rb, line 284
def reset_session
  @_request.reset_session
end

response=(response)

指派響應並將其標記為已提交。不會繼續進行後續處理程序。

# File actionpack/lib/action_controller/metal.rb, line 268
def response=(response)
  set_response!(response)

  # Force `performed?` to return true:
  @_response_body = true
end

response_body=(body)

# File actionpack/lib/action_controller/metal.rb, line 234
def response_body=(body)
  if body
    body = [body] if body.is_a?(String)
    response.body = body
    super
  else
    response.reset_body!
  end
end

session

目前請求的 ActionDispatch::Request::Session 實例。詳情請參閱 Active Controller Session 指南

# File actionpack/lib/action_controller/metal.rb, line 176
delegate :session, to: "@_request"

status

# File actionpack/lib/action_controller/metal.rb, line 196
delegate :status, to: "@_response"

status=

# File actionpack/lib/action_controller/metal.rb, line 184
delegate :status=, to: "@_response"

url_for(string)

基本的 url_for,可以被覆寫以提供更強大的功能。

# File actionpack/lib/action_controller/metal.rb, line 230
def url_for(string)
  string
end