Reputation: 267020
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
Upvotes: 4
Views: 7669
Reputation: 20263
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.
Upvotes: 14