class

ActionController::Base

Inherits ActionController::SpecHelper::ContextHelper < ActionController::Responders < ActionController::Route::Builder < Reference < Object

the base class of all controllers

Instance methods

action_name

the action name being executed (typically the function name)

Source
client_ip

attempts to find the clients IP address using proxy headers or the remote_address

Source
cookies

parses the cookies sent with the request

Source
files

returns any file provided in a multipart post

Source
form_data

returns any data that could be parsed from the request body.

It will only attempt to parse the data if the appropriate content-type header is set

Source
params

parses all the params (query + route + body form data) and makes them available in one place

Source
query_params(*args, **options)

parses the query params that were sent with the request

Source
query_params(*args, **options, &)

parses the query params that were sent with the request

Source
render_called

has the response already been sent?

Source
render_called?

has the response already been sent?

Source
request(*args, **options)

shortcuts to methods available on the context

Source
request(*args, **options, &)

shortcuts to methods available on the context

Source
request_charset

looks at the content type header to see if the charset was defined

Source
request_content_type

looks at the content type header for the media type and returns the text representation of it

Source
request_media_type

looks at the content type header for the media type

Source
request_protocol

returns either :http or :https

Source
response(*args, **options)

shortcuts to methods available on the context

Source
response(*args, **options, &)

shortcuts to methods available on the context

Source
route_params(*args, **options)

shortcuts to methods available on the context

Source
route_params(*args, **options, &)

shortcuts to methods available on the context

Source
session

loads the session from the cookies if it exists

Source
stale?(last_modified : Time | Nil = nil, etag : String | Nil = nil, public : Bool = false)

Sets the etag and/or last_modified on the response and checks it against the client request. If it’s fresh and we don’t need to generate anything, a reply of 304 Not Modified is sent.

Source

Macros

after_action(method, only = nil, except = nil, filter_name = nil)

runs code after an action is executed and the response has been sent.

@[AC::Route::Filter(:after_action)]
def audit_user_activity
  log_activity(session["user"], request_protocol, action_name)
end
Source
around_action(method, only = nil, except = nil, filter_name = nil)

wraps actions in the code provided, must yield to the action

@[AC::Route::Filter(:around_action, only: [:create, :update])]
def wrap_in_transaction(&)
  PgORM::Database.transaction do
    yield
  end
end

NOTE: unlike before_action and after_action, which compile to ordinary method calls, an around filter has to yield and so Crystal inlines its body into every route it applies to. A filter carrying a lot of code is duplicated once per route; keep the body thin and put the work in a regular method:

@[AC::Route::Filter(:around_action)]
def instrument(&)
  started = start_metrics
  yield
ensure
  record_metrics(started)
end
Source
base(name = nil)

this route is appended to any routes defined in the controller

defaults to /

Source
before_action(method, only = nil, except = nil, filter_name = nil)

runs code before an action is executed

@[AC::Route::Filter(:before_action, except: [:public_show])]
def ensure_authenticated
  raise Error::Unauthorized.new("user not found") unless session["user"]?
end
Source
delete(path, function = nil, annotations = nil, reference = nil, execution_context = nil, &block)

define a new route that responds to DELETE requests

Source
execution_context(name)

bind every route in this controller to a named response execution context.

The context must be declared with ActionController::ExecutionContext.define. A per-route execution_context: annotation takes precedence over this. Has no effect unless compiled with -Dpreview_mt -Dexecution_context.

class Reports < ActionController::Base
  execution_context "reports"
end
Source
force_ssl(only = nil, except = nil, filter_name = nil)

ensures certain routes can only be accessed over TLS

recommended that this is enabled for your entire application

Source
force_tls(only = nil, except = nil)

ensures certain routes can only be accessed over TLS

recommended that this is enabled for your entire application

Source
get(path, function = nil, annotations = nil, reference = nil, execution_context = nil, &block)

define a new route that responds to GET requests

Source
layout(filename = nil)
Source
options(path, function = nil, annotations = nil, reference = nil, execution_context = nil, &block)

define a new route that responds to OPTIONS requests

Source
partial(partial, io = nil)
Source
patch(path, function = nil, annotations = nil, reference = nil, execution_context = nil, &block)

define a new route that responds to PATCH requests

Source
post(path, function = nil, annotations = nil, reference = nil, execution_context = nil, &block)

define a new route that responds to POST requests

Source
put(path, function = nil, annotations = nil, reference = nil, execution_context = nil, &block)

define a new route that responds to PUT requests

Source
rescue_from(error_class, method = nil, &block)

provide custom responses for certain types of errors that might occur in multiple routes

helps keep your code DRY and simplifies controller methods

Source
skip_action(method, only = nil, except = nil, filter_name = nil)

provides a method for skipping filters that were defined in parent classes.

for instance, you might have a global authorisation check, however one route is less strict

skip_action :authorize!, only: [:public_show]
Source
template(template = nil, partial = nil, layout = nil, io = nil)
Source
template_path(path)
Source
ws(path, function = nil, annotations = nil, reference = nil, &block)

used to define a websocket route

Source