简体   繁体   English

ActionController :: API和ApplicationController有什么区别

[英]What is the difference between ActionController::API and ApplicationController

I have some controllers that are used for API responses, but the rest of the app is a regular Rails 5 application. 我有一些用于API响应的控制器,但该应用程序的其余部分是常规的Rails 5应用程序。

What is the difference if I change my base class to 如果我将基类更改为

ApplicationController::API

versus

ApplicationController

I noticed if I do ::API then I need to add the json: when rendering, before I could just render msg 我注意到如果我执行:: API,则需要在渲染时添加json:,然后才可以渲染msg

Behind the scenes is the request/response much leaner when inheriting from API? 从API继承时,请求/响应的背后是否更精简?

class Api::V1::TestController < ????
  def index
    msg = {status: "ok", message: "hello world"}
    render json: msg
  end
end

From the docs (which are the first hit on the googlies )... 文档 (这是在googlies上的第一击)...

API Controller is a lightweight version of ActionController::Base, created for applications that don't require all functionalities that a complete Rails controller provides, allowing you to create controllers with just the features that you need for API only applications. API Controller是ActionController :: Base的轻量级版本,是为不需要完全Rails控制器提供的所有功能的应用程序创建的,允许您创建仅具有仅API应用程序所需功能的控制器。

An API Controller is different from a normal controller in the sense that by default it doesn't include a number of features that are usually required by browser access only: layouts and templates rendering, cookies, sessions, flash, assets, and so on. API控制器与普通控制器的不同之处在于,默认情况下,它不包含通常仅浏览器访问所需的许多功能:布局和模板渲染,Cookie,会话,Flash,资产等。 This makes the entire controller stack thinner, suitable for API applications. 这使整个控制器堆栈变薄,适合API应用程序。 It doesn't mean you won't have such features if you need them: they're all available for you to include in your application, they're just not part of the default API controller stack. 这并不意味着您就不需要这些功能:可以将它们全部包含在应用程序中,它们也不是默认API控制器堆栈的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 4中的AbstractController和ActionController之间的区别 - Difference between AbstractController and ActionController in rails 4 解释ActionController#respond_to和ActionController :: respond_to之间的区别 - Explain the difference between ActionController#respond_to and ActionController::respond_to 类ApplicationController &lt;ActionController :: Base之前的include - An include before class ApplicationController < ActionController::Base ActionController :: RoutingError-未初始化的常量ApplicationController :: CanCan: - ActionController::RoutingError - uninitialized constant ApplicationController::CanCan: ActionController :: TestCase与ActiveSupport :: TestCase; 到底有什么区别? - ActionController::TestCase vs ActiveSupport::TestCase; What exactly is the difference? ruby中ApplicationController类的目的是什么 - What is purpose of ApplicationController class in ruby ApplicationController / Helper / view负责什么? - What are ApplicationController/Helper/view responsible for? 在Rails API中使用局部变量或实例变量有什么区别? - What is the difference between using local variable or instance variable in an Rails API? 常规Rails应用程序和Rails API之间有什么区别? - What is the difference between a regular Rails app and a Rails API? 符号之间有什么区别? - What is the difference between symbols?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM