我想了解一下Rails ActionController::金属控制器。我读过相关内容here http://api.rubyonrails.org/classes/ActionController/Metal.html但不完全理解。
它用于构建 API,但我们也可以在没有它的情况下构建 API。
那么它到底有什么作用以及有多大用处呢?
请问有人可以用例子解释一下吗?
ActionController::Metal 本质上是 ActionController::Base 的精简版本。它主要用于 API,因为它不包含通常附带 Rails 控制器的模块,从而提高性能(甚至可以提高 40%,具体取决于用例https://gist.github.com/drogus/738168 https://gist.github.com/drogus/738168).
鉴于它只包含最基本的控制器功能,您可以只为自己的类添加所需的功能。例如,可以添加渲染、令牌身份验证和过滤功能:
class ApiGenericController < ActionController::Metal
include ActionController::Rendering
include ActionController::Renderers::All
include ActionController::MimeResponds
include ActionController::ImplicitRender
include AbstractController::Callbacks
include ActionController::HttpAuthentication::Token::ControllerMethods
它基本上是一种确保您充分利用计算资源的快速方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)