Given the following form:
<%= form_for(@ciudad) do |f| %>
<% if @ciudad.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@ciudad.errors.count, "error") %> prohibited this ciudad from being saved:</h2>
<ul>
<% @ciudad.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :nombre %><br />
<%= f.text_field :nombre %>
</div>
<div class="field">
<%= f.label :departamento_id %><br />
<%= f.select :departamento_id , :prompt => "Seleccione el municipio" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
How would I populate the options for the deparamento_id select tag from a database?
The simplest way to do that is to use
f.collection_selectinstead off.select. Assuming you have a table / model namedDepartamentowith a field callednombre:You can read more about it in the official Rails Guides here: http://guides.rubyonrails.org/form_helpers.html#option-tags-from-a-collection-of-arbitrary-objects
And in the API documentation:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select