File: //usr/local/rvm/gems/ruby-2.7.4/doc/actionpack-6.1.4.1/ri/ActionController/API/cdesc-API.ri
U:RDoc::NormalClass[iI"API:ETI"ActionController::API;TI"
Metal;To:RDoc::Markup::Document:@parts[o;;[ o:RDoc::Markup::Paragraph;[ I"QAPI Controller is a lightweight version of <tt>ActionController::Base</tt>, ;TI"Ucreated for applications that don't require all functionalities that a complete ;TI"R\Rails controller provides, allowing you to create controllers with just the ;TI"6features that you need for API only applications.;To:RDoc::Markup::BlankLine o; ;[I"OAn API Controller is different from a normal controller in the sense that ;TI"Rby default it doesn't include a number of features that are usually required ;TI">by browser access only: layouts and templates rendering, ;TI"Oflash, assets, and so on. This makes the entire controller stack thinner, ;TI"Hsuitable for API applications. It doesn't mean you won't have such ;TI"Lfeatures if you need them: they're all available for you to include in ;TI"Qyour application, they're just not part of the default API controller stack.;T@o; ;[I"QNormally, +ApplicationController+ is the only controller that inherits from ;TI"P<tt>ActionController::API</tt>. All other controllers in turn inherit from ;TI"+ApplicationController+.;T@o; ;[I".A sample controller could look like this:;T@o:RDoc::Markup::Verbatim;[I"3class PostsController < ApplicationController
;TI" def index
;TI" posts = Post.all
;TI" render json: posts
;TI" end
;TI" end
;T:@format0o; ;[I"NRequest, response, and parameters objects all work the exact same way as ;TI"%<tt>ActionController::Base</tt>.;T@S:RDoc::Markup::Heading:
leveli: textI"Renders;T@o; ;[
I"NThe default API Controller stack includes all renderers, which means you ;TI"Qcan use <tt>render :json</tt> and brothers freely in your controllers. Keep ;TI"Pin mind that templates are not going to be rendered, so you need to ensure ;TI"Ryour controller is calling either <tt>render</tt> or <tt>redirect_to</tt> in ;TI":all actions, otherwise it will return 204 No Content.;T@o;;[ I"def show
;TI"% post = Post.find(params[:id])
;TI" render json: post
;TI" end
;T;0S;
;i;I"Redirects;T@o; ;[I"LRedirects are used to move from one action to another. You can use the ;TI"K<tt>redirect_to</tt> method in your controllers in the same way as in ;TI"2<tt>ActionController::Base</tt>. For example:;T@o;;[ I"def create
;TI": redirect_to root_url and return if not_authorized?
;TI" # do stuff here
;TI" end
;T;0S;
;i;I"Adding New Behavior;T@o; ;[I"OIn some scenarios you may want to add back some functionality provided by ;TI"G<tt>ActionController::Base</tt> that is not present by default in ;TI"N<tt>ActionController::API</tt>, for instance <tt>MimeResponds</tt>. This ;TI"Qmodule gives you the <tt>respond_to</tt> method. Adding it is quite simple, ;TI"Hyou just need to include the module in a specific controller or in ;TI"J+ApplicationController+ in case you want it available in your entire ;TI"application:;T@o;;[I"9class ApplicationController < ActionController::API
;TI". include ActionController::MimeResponds
;TI" end
;TI"
;TI"3class PostsController < ApplicationController
;TI" def index
;TI" posts = Post.all
;TI"
;TI" respond_to do |format|
;TI". format.json { render json: posts }
;TI"- format.xml { render xml: posts }
;TI"
end
;TI" end
;TI" end
;T;0o; ;[I"PMake sure to check the modules included in <tt>ActionController::Base</tt> ;TI"Eif you want to use any other functionality that is not provided ;TI"6by <tt>ActionController::API</tt> out of the box.;T:
@fileI"!lib/action_controller/api.rb;T:0@omit_headings_from_table_of_contents_below0;0;0[ [U:RDoc::Constant[i I"MODULES;TI"#ActionController::API::MODULES;T:public0o;;[ ;@n;0@n@cRDoc::NormalClass0[ [[I"
class;T[[;[[I"without_modules;TI"!lib/action_controller/api.rb;T[:protected[ [:private[ [I"
instance;T[[;[ [;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ;0;0[@nI"ActionController;TcRDoc::NormalModule