Skip to content

abstract class ActionController::Base
inherits Reference #

the base class of all controllers

Included modules

ActionController::Responders ActionController::Route::Builder

Class methods#

.handler : Composition#

Serves this controller and its descendants, passing route misses downstream.

View source

Methods#

#action_name : Symbol#

the action name being executed (typically the function name)

View source

#client_ip : String#

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

View source

#context : HTTP::Server::Context#

View source

#cookies : HTTP::Cookies#

parses the cookies sent with the request

View source

#files : Hash(String, Array(ActionController::BodyParser::FileUpload)) | ::Nil#

returns any file provided in a multipart post

View 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

View source

#params : URI::Params#

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

View source

#query_params(*args, **options)#

parses the query params that were sent with the request

View source

#query_params#

parses the query params that were sent with the request

View source

#render_called#

has the response already been sent?

View source

#render_called? : Bool#

has the response already been sent?

View source

#request(*args, **options)#

shortcuts to methods available on the context

View source

#request#

shortcuts to methods available on the context

View source

#request_charset : String#

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

View source

#request_content_type : String | ::Nil#

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

View source

#request_media_type : MIME::MediaType | ::Nil#

looks at the content type header for the media type

View source

#request_protocol#

returns either :http or :https

View source

#response(*args, **options)#

shortcuts to methods available on the context

View source

#response#

shortcuts to methods available on the context

View source

#route_params(*args, **options)#

shortcuts to methods available on the context

View source

#route_params#

shortcuts to methods available on the context

View source

#route_path(action : Symbol, hash_parts : Hash(String | Symbol, Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol | Nil) | Nil = nil, **tuple_parts) : String#

Builds a URL with this request's mounted base and bound path parameters.

View source

#session : Session#

loads the session from the cookies if it exists

View 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.

View 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
View 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
View source

base(name = nil)#

this route is appended to any routes defined in the controller

defaults to /

View 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
View source

delete(path, function = nil, annotations = nil, reference = nil, execution_context = nil, internal = false#

define a new route that responds to DELETE requests

internal: true routes are not exposed via HTTP, they're used to implement MCP prompts

View 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 execution contexts are enabled (ExecutionContext::ENABLED).

class Reports < ActionController::Base
  execution_context "reports"
end
View source

force_ssl(only = nil, except = nil, filter_name = nil)#

ensures routes can only be accessed over TLS, plain HTTP requests are redirected to HTTPS (websockets are refused). Applies to every route when neither only: nor except: is provided.

the protocol is determined by ActionController::Support.request_protocol: proxy headers, otherwise whether the connection is to a port Server bound with TLS.

recommended that this is enabled for your entire application

View source

force_tls(only = nil, except = nil)#

ensures routes can only be accessed over TLS, plain HTTP requests are redirected to HTTPS (websockets are refused). Applies to every route when neither only: nor except: is provided.

the protocol is determined by ActionController::Support.request_protocol: proxy headers, otherwise whether the connection is to a port Server bound with TLS.

recommended that this is enabled for your entire application

View source

get(path, function = nil, annotations = nil, reference = nil, execution_context = nil, internal = false#

define a new route that responds to GET requests

internal: true routes are not exposed via HTTP, they're used to implement MCP prompts

View source

layout(filename = nil)#

View source

mount(path, target)#

Mount a controller subtree with a replacement base relative to this controller.

View source

options(path, function = nil, annotations = nil, reference = nil, execution_context = nil, internal = false#

define a new route that responds to OPTIONS requests

internal: true routes are not exposed via HTTP, they're used to implement MCP prompts

View source

partial(partial, io = nil)#

View source

patch(path, function = nil, annotations = nil, reference = nil, execution_context = nil, internal = false#

define a new route that responds to PATCH requests

internal: true routes are not exposed via HTTP, they're used to implement MCP prompts

View source

post(path, function = nil, annotations = nil, reference = nil, execution_context = nil, internal = false#

define a new route that responds to POST requests

internal: true routes are not exposed via HTTP, they're used to implement MCP prompts

View source

put(path, function = nil, annotations = nil, reference = nil, execution_context = nil, internal = false#

define a new route that responds to PUT requests

internal: true routes are not exposed via HTTP, they're used to implement MCP prompts

View source

rescue_from(error_class, method = nil#

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

helps keep your code DRY and simplifies controller methods

View 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]

View source

template(template = nil, partial = nil, layout = nil, io = nil)#

View source

template_path(path)#

View source

ws(path, function = nil, annotations = nil, reference = nil#

used to define a websocket route

View source