class ActionController::API

by ActionController::API out of the box.
if you want to use any other functionality that is not provided
Make sure to check the modules included in ActionController::Base
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
application:
ApplicationController in case you want it available in your entire
you just need to include the module in a specific controller or in
module gives you the respond_to method. Adding it is quite simple,
ActionController::API, for instance MimeResponds. This
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
all actions, otherwise it will return 204 No Content.
your controller is calling either render or redirect_to in
in mind that templates are not going to be rendered, so you need to ensure
can use render :json and siblings freely in your controllers. Keep
The default API Controller stack includes all renderers, which means you
== 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
your application, they’re just not part of the default API controller stack.
features if you need them: they’re all available for you to include in
suitable for API applications. It doesn’t mean you won’t have such
flash, assets, and so on. This makes the entire controller stack thinner,
by browser access only: layouts and templates rendering,
by default it doesn’t include a number of features that are usually required
An API Controller is different from a normal controller in the sense that
features that you need for API only applications.
Rails controller provides, allowing you to create controllers with just the
created for applications that don’t require all functionalities that a complete
API Controller is a lightweight version of ActionController::Base,
= Action Controller API

def self.without_modules(*modules)

manually.
to 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

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

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