April 2019
Beginner to intermediate
244 pages
6h 34m
English
The last step of the process is sending our changeset to Repo and seeing what happens. In this section, we’ll see how changesets help us track errors and report them to the user.
Looking back at our example from the beginning of the chapter, we can see the entire changeset workflow:
| | params = %{name: "Gene Harris"} |
| | changeset = |
| | %Artist{} |
| | |> cast(params, [:name]) |
| | |> validate_required([:name]) |
| | |
| | case Repo.insert(changeset) do |
| | {:ok, artist} -> IO.puts("Record for #{artist.name} was created.") |
| | {:error, changeset} -> IO.inspect(changeset.errors) |
| | end |
This is how we handle inserting a new record (if we were updating an ...
Read now
Unlock full access