ActionController::Base
Inherits ActionController::SpecHelper::ContextHelper < ActionController::Responders < ActionController::Route::Builder < Reference < Object
the base class of all controllers
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
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
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
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
ensures certain routes can only be accessed over TLS
recommended that this is enabled for your entire application
ensures certain routes can only be accessed over TLS
recommended that this is enabled for your entire application
define a new route that responds to GET requests
define a new route that responds to OPTIONS requests
define a new route that responds to PATCH requests
define a new route that responds to POST requests
define a new route that responds to PUT requests
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