While writing a phoenix app you may want to redirect a user back to where they came
from. This is possible because HTTP sends you a refer header which tells you which page/url the user came from.
Rails allows you to this using
redirect_to :back in versions < 5 and
redirect_back(fallback: "/") in versions >= 5. You can do something similar in Phoenix by using the following code snippet.
def redirect_back(conn, fallback \\ "/") do case get_req_header(conn, "referer") do [referer] -> redirect conn, to: referer _ -> redirect conn, to: fallback end end end
Which can be used as below.
conn |> put_flash(:info, "Success!") |> redirect_back # or conn |> put_flash(:info, "Success!") |> redirect_back(_fallback = "/dash")