Rails ActionController :: Metal实际上做了什么

时间:2013-08-02 19:03:18

标签: ruby-on-rails

我想了解 Rails ActionController :: Metal 控制器。我已经阅读了here,但完全不了解它。

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

那它究竟做了什么以及它有多大用处?

可以请任何人用例子来解释它吗?

1 个答案:

答案 0 :(得分:23)

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

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