So I know it aint right to assign a variable inside a case statement, but I can't figure out how to refactor this so that the user gets redirected AND the conn gets assigned correctly:
case Repo.insert(changeset) do
{:ok,user} -> conn = put_session(conn, :user, user)
redirect conn, to: "/"
{:error, changeset} -> render(conn,"sign_up.html", changeset: changeset, referral: referral)
end
I can't just move the conn out can I? I'm not sure that assigns it right since now I'm assigning it to the whole redirect statement.
conn =
case Repo.insert(changeset) do
{:ok,user} -> redirect put_session(conn, :user, user), to: "/"
{:error, changeset} -> render(conn,"sign_up.html", changeset: changeset, referral: referral)
end
Any suggestions?
redirect
and render
both return the conn
so the second version is fine.
If the case
is the last expression in the controller function, then you might not need to re-assign conn
at all.
conn =
case Repo.insert(changeset) do
{:ok,user} ->
conn
|> put_session(conn, :user, user)
|> redirect(to: "/")
{:error, changeset} ->
conn
|> render("sign_up.html", changeset: changeset, referral: referral)
end