【问题标题】:What actually does Rails ActionController::MetalRails ActionController::Metal 实际上做了什么
【发布时间】:2017-04-05 00:00:34
【问题描述】:

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

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

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

谁能用例子解释一下?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    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-06-02
    • 2020-11-21
    • 2015-02-26
    • 2017-06-29
    • 2013-06-13
    • 2021-10-25
    • 2011-08-12
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多