跳至内容 跳至搜索

Action Controller 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 方法为 Rails 路由器分派返回一个有效的 Rack 应用程序。

渲染帮助器

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
M
N
P
R
S
U

属性

[R] 请求

ActionDispatch::Request 当前请求的实例。

[R] 响应

ActionDispatch::Response 当前响应的实例。

类公共方法

action(name)

返回给定操作名称的 Rack 端点。

# File actionpack/lib/action_controller/metal.rb, line 289
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 305
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 中间件堆栈 的更多信息。

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

new()

# File actionpack/lib/action_controller/metal.rb, line 185
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 267
def use(...)
  middleware_stack.use(...)
end

实例公共方法

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"

params()

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

params=(val)

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

performed?()

测试是否已执行渲染或重定向。

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

reset_session()

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

response=(response)

分配响应并将其标记为已提交。不会进行进一步的处理。

# File actionpack/lib/action_controller/metal.rb, line 242
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 209
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 会话指南 中的更多详细信息。

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

url_for(string)

可以覆盖以获得更强大功能的基本 url_for。

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