In a Phoenix Framework form, I have a select box on my page which has an option to set a belongs_to value to nil.
<%= select f, :relation_id,
Enum.into(Enum.map(@relations, fn p -> {p.name, p.id} end),
[{"None", nil}]) %>
The form would usually send the ID, but when the nil value is selected, it passes the value as an empty string:
"relation_id" => ""
I receive an error from Ecto that the changeset is invalid, as it expects an integer. I could probably intercept the map, set the value to null, and pass the updated map into the changeset. But is there an easier way to do this?
relations
field in your controller? Can't find an example on how to do it with a changeset. Thanks – Paco