abstract class ActionController::Base
- ActionController::Base
- Reference
- Object
Overview
the base class of all controllers
Included Modules
- ActionController::Responders
- ActionController::Route::Builder
- ActionController::SpecHelper::ContextHelper
Defined in:
action-controller/base.craction-controller/spec_helper.cr
Macro Summary
-
after_action(method, only = nil, except = nil, filter_name = nil)
runs code after an action is executed and the response has been sent.
-
around_action(method, only = nil, except = nil, filter_name = nil)
wraps actions in the code provided, must
yield
to the action -
base(name = nil)
this route is appended to any routes defined in the controller
-
before_action(method, only = nil, except = nil, filter_name = nil)
runs code before an action is executed
-
delete(path, function = nil, annotations = nil, reference = nil, &block)
define a new route that responds to DELETE requests
-
force_ssl(only = nil, except = nil, filter_name = nil)
ensures certain routes can only be accessed over TLS
-
force_tls(only = nil, except = nil)
ensures certain routes can only be accessed over TLS
-
get(path, function = nil, annotations = nil, reference = nil, &block)
define a new route that responds to GET requests
- layout(filename = nil)
-
options(path, function = nil, annotations = nil, reference = nil, &block)
define a new route that responds to OPTIONS requests
- partial(partial, io = nil)
-
patch(path, function = nil, annotations = nil, reference = nil, &block)
define a new route that responds to PATCH requests
-
post(path, function = nil, annotations = nil, reference = nil, &block)
define a new route that responds to POST requests
-
put(path, function = nil, annotations = nil, reference = nil, &block)
define a new route that responds to PUT requests
-
rescue_from(error_class, method = nil, &block)
provide custom responses for certain types of errors that might occur in multiple routes
-
skip_action(method, only = nil, except = nil, filter_name = nil)
provides a method for skipping filters that were defined in parent classes.
- template(template = nil, partial = nil, layout = nil, io = nil)
- template_path(path)
-
ws(path, function = nil, annotations = nil, reference = nil, &block)
used to define a websocket route
Instance Method Summary
-
#action_name : Symbol
the action name being executed (typically the function name)
-
#client_ip : String
attempts to find the clients IP address using proxy headers or the remote_address
-
#context : HTTP::Server::Context
the request context
-
#cookies : HTTP::Cookies
parses the cookies sent with the request
-
#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.
-
#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
-
#query_params(*args, **options, &)
parses the query params that were sent with the request
-
#render_called : Bool
has the response already been sent?
DEPRECATED use
#render_called?
-
#render_called? : Bool
has the response already been sent?
-
#request(*args, **options)
shortcuts to methods available on the context
-
#request(*args, **options, &)
shortcuts to methods available on the context
-
#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
-
#request_protocol
returns either
:http
or:https
-
#response(*args, **options)
shortcuts to methods available on the context
-
#response(*args, **options, &)
shortcuts to methods available on the context
-
#route_params(*args, **options)
shortcuts to methods available on the context
-
#route_params(*args, **options, &)
shortcuts to methods available on the context
-
#session : Session
loads the session from the cookies if it exists
-
#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.
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
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
this route is appended to any routes defined in the controller
defaults to /
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
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
Instance Method Detail
attempts to find the clients IP address using proxy headers or the remote_address
returns any file provided in a multipart post
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
looks at the content type header for the media type
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.