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

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

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

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

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

ActionController :: Metal本质上是ActionController :: Base的精简版本。 它主要用于API,因为它不包含通常带有Rails控制器的模块,从而提高性能(甚至40%,具体取决于用例https://gist.github.com/drogus/738168 )。

鉴于它只包含最基本的控制器function,您只需为自己的类添加所需的function。 例如,可以添加渲染,令牌身份validation和过滤function:

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 

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