Action Controller

Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output.

Methods and Actions

config/routes.rb app/controllers/posts_controller.rb

CRUD actions

config/routes.rb app/controllers/posts_controller.rb

Parameters

app/controllers/posts_controller.rb

Hash and Array Parameters

app/controllers/posts_controller.rb app/views/posts/_form.html.erb app/controllers/posts_controller.rb

JSON parameters

app/controllers/posts_controller.rb

Routing parameters

config/routes.rb app/controllers/posts_controller.rb

Default URL options

app/controllers/application_controller.rb

Strong parameters

app/controllers/posts_controller.rb

Session

config/initializers/session_store.rb

Accessing the session

app/controllers/application_controller.rb
app/controllers/logins_controller.rb

The Flash

app/controllers/logins_controller.rb
app/views/layouts/application.html.erb
app/controllers/posts_controller.rb
app/controllers/posts_controller.rb

Cookies

app/controllers/comments_controller.rb

Rendering XML and JSON data

app/controllers/posts_controller.rb

Before/after actions

app/controllers/application_controller.rb
app/controllers/posts_controller.rb
app/controllers/posts_controller.rb

Around actions

app/controllers/posts_controller.rb
app/controllers/application_controller.rb
app/controllers/application_controller.rb

Request object

app/controllers/application_controller.rb
Property of request Purpose
host The hostname used for this request.
domain(n=2) The hostname's first n segments, starting from the right (the TLD).
format The content type requested by the client.
method The HTTP method used for the request.
get?, post?, patch?, put?, delete?, head? Returns true if the HTTP method is GET/POST/PATCH/PUT/DELETE/HEAD.
headers Returns a hash containing the headers associated with the request.
port The port number (integer) used for the request.
protocol Returns a string containing the protocol used plus "://", for example "http://".
query_string The query string part of the URL, i.e., everything after "?".
remote_ip The IP address of the client.
url The entire URL used for the request.

Response object

app/controllers/application_controller.rb
Property of response Purpose
body This is the string of data being sent back to the client. This is most often HTML.
status The HTTP status code for the response, like 200 for a successful request or 404 for file not found.
location The URL the client is being redirected to, if any.
content_type The content type of the response.
charset The character set being used for the response. Default is "utf-8".
headers Headers used for the response.

HTTP Authentications

Basic Authentication

app/controllers/admins_controller.rb

Digest Authentication

app/controllers/admins_controller.rb

Streaming

app/controllers/posts_controller.rb

Sending files

app/controllers/posts_controller.rb

RESTful Downloads

app/controllers/posts_controller.rb config/initializers/mime_types.rb

Log Filtering

Parameters Filtering

Redirects Filtering

rescue_from

app/controllers/application_controller.rb
app/controllers/application_controller.rb

Force HTTPS protocol

app/controllers/posts_controller.rb
app/controllers/posts_controller.rb

Example

app/controllers/posts_controller.rb

Testing

spec/controllers/posts_controller_spec.rb

/

#