Skip to content

Instantly share code, notes, and snippets.

@jcelliott
jcelliott / flatten.exs
Last active August 26, 2021 16:21
Recursively flatten a list in Elixir
defmodule Flatten do
@doc """
Flattens a list by recursively flattening the head and tail of the list
"""
def flatten([head | tail]), do: flatten(head) ++ flatten(tail)
def flatten([]), do: []
def flatten(element), do: [element]
end
@laserlemon
laserlemon / introduction.md
Last active February 12, 2021 10:04
Rendering form errors for non-database-backed changesets

Introduction

Disclaimer: I'm a one-day-old Elixir/Phoenix developer.

In Sonny's training yesterday, we used an Ecto.Changeset to handle user registration (a database-backed operation). This worked perfectly for rendering the intial form and re-rendering the form with validation errors.

This is because our Workshop.RegistrationController is calling Repo.Insert which sets the changeset's :action, whether or not the insertion succeeds. Then Phoenix.HTML's form_for function appropriately sets errors on the form so they can be rendered on the page.

The Problem