3.1 The Select and Option Tags
The most generic helper is select_tag, which — as the name implies — simply generates the SELECT tag that encapsulates an options string:
<%= select_tag(:city_id, 'Lisbon...') %>
This is a start, but it doesn't dynamically create the option tags. You can generate option tags with the options_for_select helper:
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...]) %>
output:
Lisbon
Madrid
...
The first argument to options_for_select is a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will be submitted to your controller. Often this will be the id of a corresponding database object but this does not have to be the case.
Knowing this, you can combine select_tag and options_for_select to achieve the desired, complete markup:
<%= select_tag(:city_id, > options_for_select(...)) %>
options_for_select allows you to pre-select an option by passing its value.
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
output:
Lisbon
selected="selected">Madrid
...
Whenever Rails sees that the internal value of an option being generated matches this value, it will add the selected attribute to that option.