Last active
September 12, 2017 09:56
-
-
Save LostKobrakai/7f71888cf3b7b32615d950db5a832f5a to your computer and use it in GitHub Desktop.
medium post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule App.RegistrationController do | |
use App.Web, :controller | |
alias App.{Registration, Repo} | |
def new(conn, _params) do | |
changeset = Registration.changeset(%Registration{}) | |
render conn, :new, changeset: changeset | |
end | |
def create(conn, %{"registration" => registration_params}) do | |
case Registration.handle_registration(registration_params) do | |
{:ok, _} -> | |
redirect conn, to: registration_path(conn, :new) | |
{:error, _operation, error_changeset, _changes} -> | |
changeset = | |
Registration.changeset(%Registration{}) | |
|> copy_errors(error_changeset) | |
render conn, :new, changeset: %{changeset | action: :insert} | |
end | |
end | |
defp copy_errors(to, from) do | |
Enum.reduce from.errors, to, fn {field, {msg, additional}}, acc -> | |
Ecto.Changeset.add_error(acc, field, msg, additional: additional) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment