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↑
| forward(conn, controller, action, args \\ []) | Forwards the response to another controller action |
| halt!(conn, opts \\ []) | Ends the response |
| headers(conn, headers) | sets response headers |
| json(conn, data) | |
| json(conn, data, opts) | Sends a normal response with |
| not_found(conn, message \\ "Not Found") | Sends a 404 (Not found) response |
| raw(conn) | Sends response as-is. It is expected that status codes, headers, body, etc have been set by the controller action |
| redirect(conn, location, opts \\ []) | Redirects the response |
| render(conn, template \\ nil, assigns \\ [], opts \\ []) | Sends a normal response |
| static(conn, file) | reads and renders a single static file |
| status(conn, status_code) | sets connection status |
Types ↑
status_code :: 100 .. 999
headers :: [{binary, binary}]
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
Specs:
- json(Plug.Conn.t, Keyword.t | list) :: Plug.Conn.t
Specs:
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
Specs:
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