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 方法返回一个有效的 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
Attributes
| [R] | request | 当前请求的 |
| [R] | response | 当前响应的 |
类公共方法
action(name) 链接
返回给定 action 名称的 Rack 端点。
来源: 显示 | 在 GitHub 上
# 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。
返回¶ ↑
-
字符串
来源: 显示 | 在 GitHub 上
# 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 的 action。
来源: 显示 | 在 GitHub 上
# 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) 链接
来源: 显示 | 在 GitHub 上
# 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) 的更多信息。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 310 def self.middleware middleware_stack end
new() 链接
来源: 显示 | 在 GitHub 上
# 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 中间件及其参数推送到中间件堆栈的底部。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 293 def use(...) middleware_stack.use(...) end
实例公共方法
content_type 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 204 delegate :content_type, to: "@_response"
content_type= 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 192 delegate :content_type=, to: "@_response"
controller_name() 链接
委托给类的 ::controller_name。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 156 def controller_name self.class.controller_name end
headers 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 180 delegate :headers, to: "@_response"
location 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 200 delegate :location, to: "@_response"
location= 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 188 delegate :location=, to: "@_response"
media_type 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 208 delegate :media_type, to: "@_response"
params() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 219 def params @_params ||= request.parameters end
params=(val) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 223 def params=(val) @_params = val end
performed?() 链接
测试是否已执行渲染或重定向。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 245 def performed? response_body || response.committed? end
reset_session() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 284 def reset_session @_request.reset_session end
response=(response) 链接
分配响应并将其标记为已提交。不再进行进一步处理。
来源: 显示 | 在 GitHub 上
# 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) 链接
来源: 显示 | 在 GitHub 上
# 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 指南。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 176 delegate :session, to: "@_request"
status 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 196 delegate :status, to: "@_response"
status= 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 184 delegate :status=, to: "@_response"
url_for(string) 链接
基本的 url_for,可以被覆盖以获得更强大的功能。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal.rb, line 230 def url_for(string) string end