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.
Methods#
#client_ip : String#
attempts to find the clients IP address using proxy headers or the remote_address
#files : Hash(String, Array(ActionController::BodyParser::FileUpload)) | ::Nil#
returns any file provided in a multipart post
#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
#params : URI::Params#
parses all the params (query + route + body form data) and makes them available in one place
#query_params(*args, **options)#
parses the query params that were sent with the request
#request_charset : String#
looks at the content type header to see if the charset was defined
#request_content_type : String | ::Nil#
looks at the content type header for the media type and returns the text representation of it
#request_media_type : MIME::MediaType | ::Nil#
looks at the content type header for the media type
#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.
#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.
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
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
base(name = nil)#
this route is appended to any routes defined in the controller
defaults to /
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
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
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
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
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
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
mount(path, target)#
Mount a controller subtree with a replacement base relative to this controller.
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
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
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
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
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
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]
ws(path, function = nil, annotations = nil, reference = nil#
used to define a websocket route