title: How to populate a select_tag with an array of hashes?
body: <p>In a Rails 3.2 app I'm trying to add a select field that takes its data from an external API call. This data is returned as an array of hashes:</p>
  
  <pre><code>[{"name"=&gt;"NameA", "id"=&gt;"001"}, {"name"=&gt;"NameB", "id"=&gt;"002"}]
  </code></pre>
  
  <p>How can I use this data to construct a select field that looks something like:</p>
  
  <pre><code>&lt;select&gt;
    &lt;option value="001"&gt; NameA &lt;/option&gt;
    &lt;option value="002"&gt; NameB &lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Thanks to the suggestions below I've tried the following:</p>
  
  <p>A:</p>
  
  <pre><code>&lt;%= select_tag 'model[field]', options_from_collection_for_select(@hash, :id, :name) %&gt;
  </code></pre>
  
  <p>Gives an error:</p>
  
  <pre><code>undefined method `name' for {"name"=&gt;"NameA", "id"=&gt;"001"}:Hash
  </code></pre>
  
  <p>B:</p>
  
  <pre><code>&lt;%= select_tag 'model[field]', options_from_collection_for_select(@hash) %&gt;
  </code></pre>
  
  <p>Fixes the error but generates the wrong markup</p>
  
  <pre><code>&lt;option value="{"name"=&gt;"NameA", "id"=&gt;"001"}"&gt; {"name"=&gt;"NameA", "id"=&gt;"001"}&lt;/option&gt;
  </code></pre>
  
  <p>So I think my problem is formatting the array of hashes correctly, and I don't know enough about manipulating arrays of hashes to work out how to do this.</p>
  
  <p>Unless I'm looking in completly the worng direction, I think the key to this problem is to reformat the array at the top of this question to give: </p>
  
  <pre><code>{"NameA" =&gt;"001", "NameB" =&gt;"002"}
  </code></pre>
  
  <p>Is this even possible? And if so, how?</p>