abstract class ActionController::Base

Overview

the base class of all controllers

Included Modules

Defined in:

action-controller/base.cr
action-controller/spec_helper.cr

Macro Summary

Instance Method Summary

Instance methods inherited from module ActionController::Responders

accepts_formats : Array(String) accepts_formats

Macros inherited from module ActionController::Responders

head(status) head, redirect_to(path, status = :found) redirect_to, render(status = :ok, head = nil, json = nil, yaml = nil, xml = nil, html = nil, text = nil, binary = nil, template = nil, partial = nil, layout = nil) render, respond_with(status = :ok, &block) respond_with

Macros inherited from module ActionController::Route::Builder

add_parser(content_type, &block) add_parser, add_responder(content_type, &block) add_responder, default_parser(content_type) default_parser, default_responder(content_type) default_responder

Macro Detail

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

[View source]
macro base(name = nil) #

this route is appended to any routes defined in the controller

defaults to /


[View source]
macro 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]
macro delete(path, function = nil, annotations = nil, reference = nil, &block) #

define a new route that responds to DELETE requests


[View source]
macro 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


[View source]
macro force_tls(only = nil, except = nil) #

ensures certain routes can only be accessed over TLS

recommended that this is enabled for your entire application


[View source]
macro get(path, function = nil, annotations = nil, reference = nil, &block) #

define a new route that responds to GET requests


[View source]
macro layout(filename = nil) #

[View source]
macro options(path, function = nil, annotations = nil, reference = nil, &block) #

define a new route that responds to OPTIONS requests


[View source]
macro partial(partial, io = nil) #

[View source]
macro patch(path, function = nil, annotations = nil, reference = nil, &block) #

define a new route that responds to PATCH requests


[View source]
macro post(path, function = nil, annotations = nil, reference = nil, &block) #

define a new route that responds to POST requests


[View source]
macro put(path, function = nil, annotations = nil, reference = nil, &block) #

define a new route that responds to PUT requests


[View source]
macro 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


[View source]
macro 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]
macro template(template = nil, partial = nil, layout = nil, io = nil) #

[View source]
macro template_path(path) #

[View source]
macro ws(path, function = nil, annotations = nil, reference = nil, &block) #

used to define a websocket route


[View source]

Instance Method Detail

def action_name : Symbol #

the action name being executed (typically the function name)


[View source]
def client_ip : String #

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


[View source]
def context : HTTP::Server::Context #

[View source]
def cookies : HTTP::Cookies #

parses the cookies sent with the request


[View source]
def files : Hash(String, Array(ActionController::BodyParser::FileUpload)) | Nil #

returns any file provided in a multipart post


[View source]
def 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]
def params : URI::Params #

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


[View source]
def query_params(*args, **options) #

parses the query params that were sent with the request


[View source]
def query_params(*args, **options, &) #

parses the query params that were sent with the request


[View source]
def render_called : Bool #

has the response already been sent?

DEPRECATED use #render_called?


[View source]
def render_called? : Bool #

has the response already been sent?


[View source]
def request(*args, **options) #

shortcuts to methods available on the context


[View source]
def request(*args, **options, &) #

shortcuts to methods available on the context


[View source]
def request_charset : String #

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


[View source]
def request_content_type : String | Nil #

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


[View source]
def request_media_type : MIME::MediaType | Nil #

looks at the content type header for the media type


[View source]
def request_protocol #

returns either :http or :https


[View source]
def response(*args, **options) #

shortcuts to methods available on the context


[View source]
def response(*args, **options, &) #

shortcuts to methods available on the context


[View source]
def route_params(*args, **options) #

shortcuts to methods available on the context


[View source]
def route_params(*args, **options, &) #

shortcuts to methods available on the context


[View source]
def session : Session #

loads the session from the cookies if it exists


[View source]
def 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]