[英]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.