Show empty as default from dropdown

Hey I have this code:

<div class="form-group">
    <%= label f, :team_id, class: "control-label" %>
    <%= select f, :team_id, @teams, class: "form-control" %>
    <%= error_tag f, :team_id %>
  </div>

which displays a dropdown from my items from the database

I get them like this:

teams =
      Repo.all(Userteam1.Team)
      |> Enum.map(&{&1.name, &1.id})

When i do not have team the dropdown is obviously empty, but when i have even one team i cannot select the dropdown to be empty. is there a way to do so?

1 Like

Empty is just the empty string, so add an entry with an empty string, like add this right after your teams = ... bit:

teams = [{"", ""} | teams]

:slight_smile:

2 Likes

FWIW select also has a prompt option that can be used to do the same thing.

https://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html#select/4-options

3 Likes