简体   繁体   中英

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.

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

Behind the scenes is the request/response much leaner when inheriting from 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 )...

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.

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. This makes the entire controller stack thinner, suitable for API applications. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM