Understanding redirects
One of the most used Phoenix.Controller
functions is redirect/2
. This is often used at the controller level to redirect %Plug.Conn{}
to a different route after setting a flash message. The following is an example of a controller action using redirect/2
:
defmodule RedirectExampleController do use RedirectExampleWeb, :controller def show(conn, show_params) do show_info = ShowInfo.get(show_params) render(conn, "show.html", show_info: show_info) end def create(conn, create_params) do created = Creator.create(create_params) conn |> put_flash(:info, "Successfully created!") |> redirect(to: Routes.redirect_example_path(conn, :show, created)) end end
In the preceding Phoenix controller, there...