ActionController::Base
Inherits ActionController::SpecHelper::ContextHelper < ActionController::Responders < ActionController::Route::Builder < Reference < Object
the base class of all controllers
Class methods
Instance methods
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
the MCP request this route is running for, nil for regular HTTP requests.
See ActionController::MCPServer::RequestContext
parses all the params (query + route + body form data) and makes them available in one place
looks at the content type header for the media type and returns the text representation of it
Builds a URL with this request's mounted base and bound path parameters.
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
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
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
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
define a new route that responds to DELETE requests
internal: true routes are not exposed via HTTP, they're used to implement MCP prompts
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
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
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
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 a controller subtree with a replacement base relative to this controller.
define a new route that responds to OPTIONS requests
internal: true routes are not exposed via HTTP, they're used to implement MCP prompts
define a new route that responds to PATCH requests
internal: true routes are not exposed via HTTP, they're used to implement MCP prompts
define a new route that responds to POST requests
internal: true routes are not exposed via HTTP, they're used to implement MCP prompts
define a new route that responds to PUT requests
internal: true routes are not exposed via HTTP, they're used to implement MCP prompts
provide custom responses for certain types of errors that might occur in multiple routes
helps keep your code DRY and simplifies controller methods
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]
used to define a websocket route