class ActionController::API

out of the box.
to use any other functionality that is not provided by ‘ActionController::API`
Make sure to check the modules included in ActionController::Base if you want
end
end
end
format.xml { render xml: posts }
format.json { render json: posts }
respond_to do |format|
posts = Post.all
def index
class PostsController < ApplicationController
end
include ActionController::MimeResponds
class ApplicationController < ActionController::API
want it available in your entire application:
the module in a specific controller or in `ApplicationController` in case you
the `respond_to` method. Adding it is quite simple, you just need to include
`ActionController::API`, for instance `MimeResponds`. This module gives you
ActionController::Base that is not present by default in
In some scenarios you may want to add back some functionality provided by
## Adding New Behavior
end
# do stuff here
redirect_to root_url and return if not_authorized?
def create
ActionController::Base. For example:
`redirect_to` method in your controllers in the same way as in
Redirects are used to move from one action to another. You can use the
## Redirects
end
render json: post
post = Post.find(params)
def show
return `204 No Content`.
is calling either `render` or `redirect_to` in all actions, otherwise it will
templates are not going to be rendered, so you need to ensure your controller
use `render :json` and siblings freely in your controllers. Keep in mind that
The default API Controller stack includes all renderers, which means you can
## Renders
ActionController::Base.
Request, response, and parameters objects all work the exact same way as
end
end
render json: posts
posts = Post.all
def index
class PostsController < ApplicationController
A sample controller could look like this:
`ApplicationController`.
`ActionController::API`. All other controllers in turn inherit from
Normally, `ApplicationController` is the only controller that inherits from
part of the default API controller stack.
they’re all available for you to include in your application, they’re just not
applications. It doesn’t mean you won’t have such features if you need them:
on. This makes the entire controller stack thinner, suitable for API
browser access only: layouts and templates rendering, flash, assets, and so
default it doesn’t include a number of features that are usually required by
An API Controller is different from a normal controller in the sense that by
that you need for API only applications.
controller provides, allowing you to create controllers with just the features
applications that don’t require all functionalities that a complete Rails
API Controller is a lightweight version of ActionController::Base, created for
# Action Controller API

def self.without_modules(*modules)

manually.
create an API controller class, instead of listing the modules required
This gives better control over what you want to exclude and makes it easier to

end
end
include left
ActionController::API.without_modules(:UrlFor).each do |left|
class MyAPIBaseController < ActionController::Metal

ones passed as arguments:
Shortcut helper that returns all the ActionController::API modules except the
def self.without_modules(*modules)
  modules = modules.map do |m|
    m.is_a?(Symbol) ? ActionController.const_get(m) : m
  end
  MODULES - modules
end