title: How to Create a Select Tag with Options and Values from a Separate Model in the Phoenix Framework
body: <p>I've got a relatively simple form with a select tag (drop down form field).</p>
  
  <p>The options/values for the select tag are dynamic and should be updated when my Categories model is updated. </p>
  
  <p>Here's what I've got now:</p>
  
  <p>The form is rendered through the <code>web/templates/posts/new.html.eex</code> template like this:</p>
  
  <pre><code>&lt;%= render "form.html", changeset: @changeset,
                          action: project_path(@conn, :create) %&gt;
  </code></pre>
  
  <p>Here's the relevant form field in <code>web/templates/posts/form.html.eex</code></p>
  
  <pre><code>  &lt;div class="form-group"&gt;
      &lt;%= select f, :category, MyApp.Category, class: "form-control" %&gt;
      &lt;%= error_tag f, :category %&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>But I'm getting the following error:</p>
  
  <pre><code>protocol Enumerable not implemented for MyApp.Category
  </code></pre>
  
  <p>How do I use my Categories repository as select tag options in a form? </p>
  
  <p>EDIT: I've fetched the categories in my PostController's new action as suggested by Gazler (thanks Gaz).</p>
  
  <pre><code>def new
    categories = Repo.all(MyApp.Category)
    changeset = 
    user
    |&gt; build_assoc(:projects)
    |&gt; Project.changeset()
    render(conn, "new.html", changeset: changeset)
  end
  </code></pre>
  
  <p>and updated my template:</p>
  
  <pre><code>  &lt;div class="form-group"&gt;
      &lt;%= select f, :category, @categories, class: "form-control" %&gt;
      &lt;%= error_tag f, :category %&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>and now I'm getting the following error:</p>
  
  <pre><code>assign @categories not available in eex template.
  </code></pre>
  
  <p>EDIT: It looks like maybe I should be using <code>multiple_select/4</code> as <a href="https://stackoverflow.com/questions/31373730/how-to-post-a-collection-of-ids-with-multiple-select-form-field-with-phoenix-htm">Aaron did here</a> because I'd like the options to show the category title and the value to be the category ID.</p>
  
  <p>ANSWER:</p>
  
  <p>Ended up using this line from the link that Gazler posted (in my post_controller's new action):</p>
  
  <pre><code>categories = Repo.all(Qlc.Category) |&gt; Enum.map(&amp;{&amp;1.title, &amp;1.id})
  </code></pre>