I have an app in which I list parents and children. When I add a child, I need to get a list of parents to be shows as a drop list. Is there something like a collection_select
in Rails available in Phoenix? I would like to display the name of the user and use user_id as a param.
I also want to scope the list of parents by site_id.
You can use the select/4 function from
Phoenix.HTML.Form
.In your controller you can fetch fetch the parents with a query:
The reason we need this query is to format the values in the expected format:
You can then use
select/4
in your template:You can also transform the records using Enum.map/2 if you already have the parents:
controller
eex