Class: ActionController::Base
Overview
Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either it renders a template or redirects to another action. An action is defined as a public method on the controller, which will automatically be made accessible to the web-server through Rails Routes.
By default, only the ApplicationController in a Rails application inherits from Base
. All other controllers in turn inherit from ApplicationController. This gives you one class to configure things such as request forgery protection and filtering of sensitive request parameters.
A sample controller could look like this:
class PostsController < ApplicationController
def index
@posts = Post.all
end
def create
@post = Post.create params[:post]
redirect_to posts_path
end
end
Actions, by default, render a template in the app/views
directory corresponding to the name of the controller and action after executing code in the action. For example, the index
action of the PostsController would render the template app/views/posts/index.html.erb
by default after populating the @posts
instance variable.
Unlike index, the create action will not render a template. After performing its main purpose (creating a new post), it initiates a redirect instead. This redirect works by returning an external “302 Moved” HTTP response that takes the user to the index action.
These two methods represent the two basic action archetypes used in Action Controllers. Get-and-show and do-and-redirect. Most actions are variations on these themes.
Requests
For every request, the router determines the value of the controller
and action
keys. These determine which controller and action are called. The remaining request parameters, the session (if one is available), and the full request with all the HTTP headers are made available to the action through accessor methods. Then the action is performed.
The full request object is available via the request accessor and is primarily used to query for HTTP headers:
def server_ip
location = request.env["REMOTE_ADDR"]
render plain: "This server hosted at #{location}"
end
Parameters
All request parameters, whether they come from a GET or POST request, or from the URL, are available through the params method which returns a hash. For example, an action that was performed through /posts?category=All&limit=5
will include { "category" => "All", "limit" => "5" }
in params.
It's also possible to construct multi-dimensional parameter hashes by specifying keys using brackets, such as:
<input type="text" name="post[name]" value="david">
<input type="text" name="post[address]" value="hyacintvej">
A request stemming from a form holding these inputs will include { "post" => { "name" => "david", "address" => "hyacintvej" } }
. If the address input had been named post[address][street]
, the params would have included { "post" => { "address" => { "street" => "hyacintvej" } } }
. There's no limit to the depth of the nesting.
Sessions
Sessions allow you to store objects in between requests. This is useful for objects that are not yet ready to be persisted, such as a Signup object constructed in a multi-paged process, or objects that don't change much and are needed all the time, such as a User object for a system that requires login. The session should not be used, however, as a cache for objects where it's likely they could be changed unknowingly. It's usually too much work to keep it all synchronized – something databases already excel at.
You can place objects in the session by using the session
method, which accesses a hash:
session[:person] = Person.authenticate(user_name, password)
And retrieved again through the same hash:
Hello #{session[:person]}
For removing objects from the session, you can either assign a single key to nil
:
# removes :person from session
session[:person] = nil
or you can remove the entire session with reset_session
.
Sessions are stored by default in a browser cookie that's cryptographically signed, but unencrypted. This prevents the user from tampering with the session but also allows them to see its contents.
Do not put secret information in cookie-based sessions!
Responses
Each action results in a response, which holds the headers and document to be sent to the user's browser. The actual response object is generated automatically through the use of renders and redirects and requires no user intervention.
Renders
Action Controller sends content to the user by using one of five rendering methods. The most versatile and common is the rendering of a template. Included in the Action Pack is the Action View, which enables rendering of ::ERB templates. It's automatically configured. The controller passes objects to the view by assigning instance variables:
def show
@post = Post.find(params[:id])
end
Which are then automatically available to the view:
Title: <%= @post.title %>
You don't have to rely on the automated rendering. For example, actions that could result in the rendering of different templates will use the manual rendering methods:
def search
@results = Search.find(params[:query])
case @results.count
when 0 then render action: "no_results"
when 1 then render action: "show"
when 2..10 then render action: "show_many"
end
end
Read more about writing ::ERB and Builder templates in ::ActionView::Base.
Redirects
Redirects are used to move from one action to another. For example, after a create
action, which stores a blog entry to the database, we might like to show the user the new entry. Because we're following good DRY principles (Don't Repeat Yourself), we're going to reuse (and redirect to) a show
action that we'll assume has already been created. The code might look like this:
def create
@entry = Entry.new(params[:entry])
if @entry.save
# The entry was saved correctly, redirect to show
redirect_to action: 'show', id: @entry.id
else
# things didn't go so well, do something else
end
end
In this case, after saving our new entry to the database, the user is redirected to the show
method, which is then executed. Note that this is an external HTTP-level redirection which will cause the browser to make a second request (a GET to the show action), and not some internal re-routing which calls both “create” and then “show” within one request.
Learn more about redirect_to
and what options you have in Redirecting.
Calling multiple redirects or renders
An action may contain only a single render or a single redirect. Attempting to try to do either again will result in a DoubleRenderError:
def do_something
redirect_to action: "elsewhere"
render action: "overthere" # raises DoubleRenderError
end
If you need to redirect on the condition of something, then be sure to add “and return” to halt execution.
def do_something
redirect_to(action: "elsewhere") and return if monkeys.nil?
render action: "overthere" # won't be called if monkeys is nil
end
Constant Summary
-
MODULES =
# File 'actionpack/lib/action_controller/base.rb', line 203[ AbstractController::Rendering, AbstractController::Translation, AbstractController::AssetPaths, Helpers, HideActions, UrlFor, Redirecting, ActionView::Layouts, Rendering, Renderers::All, ConditionalGet, EtagWithTemplateDigest, RackDelegation, Caching, MimeResponds, ImplicitRender, StrongParameters, Cookies, Flash, RequestForgeryProtection, ForceSSL, Streaming, DataStreaming, HttpAuthentication::Basic::ControllerMethods, HttpAuthentication::Digest::ControllerMethods, HttpAuthentication::Token::ControllerMethods, # Before callbacks should also be executed the earliest as possible, so # also include them at the bottom. AbstractController::Callbacks, # Append rescue at the bottom to wrap as much as possible. Rescue, # Add instrumentations hooks at the bottom, to ensure they instrument # all the methods properly. Instrumentation, # Params wrapper should come before instrumentation so they are # properly showed in logs ParamsWrapper ]
-
PROTECTED_IVARS =
Define some internal variables that should not be propagated to the view.
AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES + [ :@_status, :@_headers, :@_params, :@_env, :@_response, :@_request, :@_view_runtime, :@_stream, :@_url_options, :@_action_has_layout ]
::AbstractController::Rendering - Included
DEFAULT_PROTECTED_INSTANCE_VARIABLES
Rendering - Included
Renderers - Included
::ActiveSupport::Callbacks - Included
RequestForgeryProtection - Included
ForceSSL - Included
ACTION_OPTIONS, REDIRECT_OPTIONS, URL_OPTIONS
ParamsWrapper - Included
Class Attribute Summary
Metal - Inherited
::AbstractController::Base - Inherited
.abstract? | Alias for AbstractController::Base.abstract. |
.supports_path? | Returns true if the given controller is capable of rendering a path. |
Class Method Summary
- .protected_instance_variables
-
.without_modules(*modules)
Shortcut helper that returns all the modules included in
Base
except the ones passed as arguments:
Metal - Inherited
.action | Returns a ::Rack endpoint for the given action name. |
.call | Makes the controller a ::Rack endpoint that runs the action in the given |
.controller_name | Returns the last part of the controller's name, underscored, without the ending |
.middleware | Alias for |
.new, | |
.use | Pushes the given ::Rack middleware and its arguments to the bottom of the middleware stack. |
::AbstractController::Base - Inherited
.abstract, | |
.abstract! | Define a controller as abstract. |
.action_methods | A list of method names that should be considered actions. |
.clear_action_methods! | action_methods are cached and there is sometimes need to refresh them. |
.controller_path | Returns the full controller name, underscored, without the ending Controller. |
.hidden_actions | The list of hidden actions. |
.internal_methods | A list of all internal methods for a controller. |
.method_added | Refresh the cached action_methods when a new action_method is added. |
::ActiveSupport::DescendantsTracker - Extended
Instance Attribute Summary
Instrumentation - Included
Rescue - Included
#rescue_handlers, #rescue_handlers?, | |
#show_detailed_exceptions? | Override this method if you want to customize when detailed exceptions must be shown. |
RequestForgeryProtection - Included
StrongParameters - Included
#params | Returns a new Parameters object that has been instantiated with the |
#params= | Assigns the given |
#rescue_handlers, #rescue_handlers? |
Caching::ConfigMethods - Included
EtagWithTemplateDigest - Included
Renderers::All - Included
::ActionView::Layouts - Included
#_layout_conditions, #action_has_layout=, | |
#action_has_layout? | Controls whether an action should be rendered using a layout. |
::ActionView::Rendering - Included
Redirecting - Included
RackDelegation - Included
::AbstractController::UrlFor - Included
Helpers - Included
::AbstractController::Rendering - Included
::ActionView::ViewPaths - Included
Metal - Inherited
#content_type, | |
#content_type= | Basic implementations for content_type=, location=, and headers are provided to reduce the dependency on the RackDelegation module in Renderer and Redirector. |
#env, #env=, | |
#headers | The details below can be overridden to support a specific Request and Response object. |
#location, #location=, #middleware_stack, #middleware_stack?, #params, #params=, | |
#performed? | Tests if render or redirect has already happened. |
#request | The details below can be overridden to support a specific Request and Response object. |
#response | The details below can be overridden to support a specific Request and Response object. |
#response_body=, #session, #status, #status= |
::AbstractController::Base - Inherited
Instance Method Summary
-
#request
Returns an ::ActionDispatch::Request instance that represents the current request.
-
#response
Returns an ::ActionDispatch::Response that represents the current response.
ParamsWrapper - Included
#process_action | Performs parameters wrapping upon the request. |
Instrumentation - Included
Rescue - Included
HttpAuthentication::Token::ControllerMethods - Included
#authenticate_or_request_with_http_token, #authenticate_with_http_token, #request_http_token_authentication |
HttpAuthentication::Digest::ControllerMethods - Included
#authenticate_or_request_with_http_digest, | |
#authenticate_with_http_digest | Authenticate with HTTP Digest, returns true or false. |
#request_http_digest_authentication | Render output including the HTTP Digest authentication header. |
HttpAuthentication::Basic::ControllerMethods - Included
#authenticate_or_request_with_http_basic, #authenticate_with_http_basic, #request_http_basic_authentication |
DataStreaming - Included
#send_data | Sends the given binary data to the browser. |
#send_file | Sends the file. |
ForceSSL - Included
#force_ssl_redirect | Redirect the existing request to use the HTTPS protocol. |
Flash - Included
::ActiveSupport::Rescuable - Included
#handler_for_rescue, | |
#rescue_with_handler | Tries to rescue the exception by looking up and calling a registered handler. |
ImplicitRender - Included
MimeResponds - Included
#respond_to | Without web-service support, an action which collects the data for displaying a list of people might look something like this: |
#respond_with |
Caching - Included
Caching::Fragments - Included
#expire_fragment | Removes fragments from the cache. |
#fragment_cache_key | Given a key (as described in |
#fragment_exist? | Check if a cached fragment from the location signified by |
#read_fragment | Reads a cached fragment from the location signified by |
#write_fragment | Writes |
::AbstractController::Callbacks - Included
#process_action | Override AbstractController::Base's process_action to run the process_action callbacks around the normal behavior. |
::ActiveSupport::Callbacks - Included
#run_callbacks | Runs the callbacks for the given event. |
ConditionalGet - Included
#expires_in | Sets a HTTP 1.1 Cache-Control header. |
#expires_now | Sets a HTTP 1.1 Cache-Control header of |
#fresh_when | Sets the |
#stale? | Sets the |
Head - Included
#head | Returns a response that has no content (merely headers). |
Renderers - Included
Rendering - Included
#render_to_body, | |
#render_to_string | Overwrite render_to_string because body can now be set to a rack body. |
::ActionView::Rendering - Included
#render_to_body, #rendered_format, | |
#view_context | An instance of a view class. |
#view_renderer | Returns an object that is able to render templates. |
Redirecting - Included
#redirect_to | Redirects the browser to the target specified in |
RackDelegation - Included
::ActiveSupport::Benchmarkable - Included
#benchmark | Allows you to measure the execution time of a block in a template and records the result to the log. |
UrlFor - Included
::AbstractController::UrlFor - Included
::ActionDispatch::Routing::UrlFor - Included
#initialize, | |
#url_for | Generate a url based on the options provided, default_url_options and the routes defined in routes.rb. |
#url_options | Hook overridden in controller to add request information with |
::ActionDispatch::Routing::PolymorphicRoutes - Included
#polymorphic_path | Returns the path component of a URL for the given record. |
#polymorphic_url | Constructs a call to a named RESTful route for the given record and returns the resulting URL string. |
ModelNaming - Included
#convert_to_model | Converts the given object to an ::ActiveModel compliant one. |
#model_name_from_record_or_class |
::AbstractController::Translation - Included
#l | Alias for AbstractController::Translation#localize. |
#localize | Delegates to |
#t | Alias for AbstractController::Translation#translate. |
#translate | Delegates to |
::AbstractController::Rendering - Included
#_normalize_args | Normalize args by converting render “foo” to render |
#_normalize_options | Normalize options. |
#_normalize_render | Normalize args and options. |
#_process_format | Process the rendered format. |
#_process_options | Process extra options. |
#render | Normalize arguments, options and then delegates render_to_body and sticks the result in self.response_body. |
#render_to_body | Performs the actual template rendering. |
#render_to_string | Raw rendering of a template to a string. |
#rendered_format | Returns Content-Type of rendered content :api: public. |
#view_assigns | This method should return a hash with assigns. |
::ActionView::ViewPaths - Included
#append_view_path, #details_for_lookup, | |
#lookup_context | LookupContext is the object responsible to hold all information required to lookup templates, i.e. view paths and details. |
#prepend_view_path, #template_exists? |
Metal - Inherited
#controller_name | Delegates to the class' |
#url_for | Basic url_for that can be overridden for more robust functionality. |
::AbstractController::Base - Inherited
#action_methods | Delegates to the class' |
#available_action? | Returns true if a method for the action is available and can be dispatched, false otherwise. |
#controller_path | Delegates to the class' |
#process | Calls the action going through the entire action dispatch stack. |
::ActiveSupport::Configurable - Included
#config | Reads and writes attributes from a configuration |
Constructor Details
This class inherits a constructor from ActionController::Metal
Class Method Details
.protected_instance_variables
[ GitHub ]# File 'actionpack/lib/action_controller/base.rb', line 262
def self.protected_instance_variables PROTECTED_IVARS end
.without_modules(*modules)
Shortcut helper that returns all the modules included in Base
except the ones passed as arguments:
class MyBaseController < ActionController::Metal
ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
include left
end
end
This gives better control over what you want to exclude and makes it easier to create a bare controller class, instead of listing the modules required manually.
# File 'actionpack/lib/action_controller/base.rb', line 195
def self.without_modules(*modules) modules = modules.map do |m| m.is_a?(Symbol) ? ActionController.const_get(m) : m end MODULES - modules end
Instance Method Details
#request
Returns an ::ActionDispatch::Request instance that represents the current request.
# File 'actionpack/lib/action_controller/base.rb', line 172
rdoc_method :method: request
#response
Returns an ::ActionDispatch::Response that represents the current response.
# File 'actionpack/lib/action_controller/base.rb', line 178
rdoc_method :method: response