13

我想了解Rails ActionController::Metal控制器。我在这里读过它,但不完全理解。

它用于构建 API,但我们也可以在没有它的情况下构建 API。

那么它到底做了什么,它有多大用处?

请任何人用例子解释一下吗?

4

1 回答 1

27

ActionController::Metal 本质上是 ActionController::Base 的精简版。它主要用于 API,因为它不包含 Rails 控制器通常附带的模块,从而提高了性能(甚至 40%,具体取决于用例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

这基本上是一种确保您充分利用计算资源的快速方法。

于 2013-08-12T14:27:30.130 回答