Sugar v0.4.11 Sugar.Controller.Helpers
All controller actions should have an arrity of 2, with the
first argument being a Plug.Conn representing the current
connection and the second argument being a Keyword list
of any parameters captured in the route path.
Sugar bundles these response helpers to assist in sending a response:
render/4-conn,template_key,assigns,opts- sends a normal response.halt!/2-conn,opts- ends the response.not_found/1-conn,message- sends a 404 (Not found) response.json/2-conn,data- sends a normal response withdataencoded as JSON.raw/1-conn- sends response as-is. It is expected that status codes, headers, body, etc have been set by the controller action.static/2-conn,file- reads and renders a single static file.
Example
defmodule Hello do
use Sugar.Controller
def index(conn, []) do
render(conn, "showing index controller")
end
def show(conn, args) do
render(conn, "showing page #{args[:id]}")
end
def create(conn, []) do
render(conn, "page created")
end
def get_json(conn, []) do
json(conn, [message: "foobar"])
end
end
Summary
Functions
Forwards the response to another controller action
Ends the response
sets response headers
Sends a normal response with data encoded as JSON
Sends a 404 (Not found) response
Sends response as-is. It is expected that status codes, headers, body, etc have been set by the controller action
Redirects the response
Sends a normal response
reads and renders a single static file
sets connection status
Types
headers :: [{binary, binary}]
status_code :: 100..999
Functions
Specs
forward(Plug.Conn.t, atom, atom, Keyword.t) :: Plug.Conn.t
Forwards the response to another controller action.
Arguments
conn-Plug.Conncontroller-Atomaction-Atomargs-Keyword
Returns
Plug.Conn
Specs
halt!(Plug.Conn.t, Keyword.t) :: Plug.Conn.t
Ends the response.
Arguments
conn-Plug.Connopts-Keyword
Returns
Plug.Conn
Specs
headers(Plug.Conn.t, headers) :: Plug.Conn.t
sets response headers
Arguments
conn-Plug.Connstatus_code-List
Returns
Plug.Conn
Sends a normal response with data encoded as JSON.
Arguments
conn-Plug.Conndata-Keyword|List
Returns
Plug.Conn
Specs
not_found(Plug.Conn.t, binary) :: Plug.Conn.t
Sends a 404 (Not found) response.
Arguments
conn-Plug.Conn
Returns
Plug.Conn
Specs
raw(Plug.Conn.t) :: Plug.Conn.t
Sends response as-is. It is expected that status codes, headers, body, etc have been set by the controller action.
Arguments
conn-Plug.Conn
Returns
Plug.Conn
Specs
redirect(Plug.Conn.t, binary, Keyword.t) :: Plug.Conn.t
Redirects the response.
Arguments
conn-Plug.Connlocation-Stringopts-Keyword
Returns
Plug.Conn
Sends a normal response.
Automatically renders a template based on the current controller and action names when no template is passed.
Arguments
conn-Plug.Conntemplate_key-Stringassigns-Keywordopts-Keyword
Returns
Plug.Conn
Specs
static(Plug.Conn.t, binary) :: Plug.Conn.t
reads and renders a single static file.
Arguments
conn-Plug.Connfile-String
Returns
Plug.Conn
Specs
status(Plug.Conn.t, status_code) :: Plug.Conn.t
sets connection status
Arguments
conn-Plug.Connstatus_code-Integer
Returns
Plug.Conn