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:

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
Source

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 data encoded as JSON

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

forward(conn, controller, action, args \\ [])

Specs:

  • forward(Plug.Conn.t, atom, atom, Keyword.t) :: Plug.Conn.t

Forwards the response to another controller action.

Arguments

  • conn - Plug.Conn
  • controller - Atom
  • action - Atom
  • args - Keyword

Returns

Plug.Conn

Source
halt!(conn, opts \\ [])

Specs:

  • halt!(Plug.Conn.t, Keyword.t) :: Plug.Conn.t

Ends the response.

Arguments

  • conn - Plug.Conn
  • opts - Keyword

Returns

Plug.Conn

Source
headers(conn, headers)

Specs:

  • headers(Plug.Conn.t, headers) :: Plug.Conn.t

sets response headers

Arguments

  • conn - Plug.Conn
  • status_code - List

Returns

Plug.Conn

Source
json(conn, data)

Specs:

  • json(Plug.Conn.t, Keyword.t | list) :: Plug.Conn.t
Source
json(conn, data, opts)

Specs:

Sends a normal response with data encoded as JSON.

Arguments

  • conn - Plug.Conn
  • data - Keyword|List

Returns

Plug.Conn

Source
not_found(conn, message \\ "Not Found")

Specs:

  • not_found(Plug.Conn.t, binary) :: Plug.Conn.t

Sends a 404 (Not found) response.

Arguments

  • conn - Plug.Conn

Returns

Plug.Conn

Source
raw(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

Source
redirect(conn, location, opts \\ [])

Specs:

  • redirect(Plug.Conn.t, binary, Keyword.t) :: Plug.Conn.t

Redirects the response.

Arguments

  • conn - Plug.Conn
  • location - String
  • opts - Keyword

Returns

Plug.Conn

Source
render(conn, template \\ nil, assigns \\ [], opts \\ [])

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.Conn
  • template_key - String
  • assigns - Keyword
  • opts - Keyword

Returns

Plug.Conn

Source
static(conn, file)

Specs:

  • static(Plug.Conn.t, binary) :: Plug.Conn.t

reads and renders a single static file.

Arguments

  • conn - Plug.Conn
  • file - String

Returns

Plug.Conn

Source
status(conn, status_code)

Specs:

sets connection status

Arguments

  • conn - Plug.Conn
  • status_code - Integer

Returns

Plug.Conn

Source