You can use the select / 4 function of Phoenix.HTML.Form .
In your controller, you can get selections to parents asking:
query = from(p in Parent, select: {p.id, p.name}) parents = Repo.all(query)
The reason we need this request is to format the values ββin the expected format:
The values ββare expected to be enumerable, containing two-position tuples (for example, maps and keyword lists) or any enumerable in which the element will be used as a key and value for the generated selection.
Then you can use select/4 in your template:
<%= select f, :parent_id, @parents ,class: "form-control" %>
You can also convert records using Enum.map/2 if you already have parents:
parents = Repo.all(Parent) |> Enum.map(&{&1.id, &1.name})
source share