form.select

Rails formで使用するときはこう。

<%= form_with model: Model do |f|  %>
  <%= f.select プロパティ名, 選択肢の配列 %>
<% end %>

select_tag

select_tag でoptionを生成。

select_tag "people", options_from_collection_for_select(@people, "id", "name")
# <select id="people" name="people"><option value="1">David</option></select>

選択肢の配列の実装

options_for_select

options_for_select が便利。

options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])
# => <option value="$">Dollar</option>
# => <option value="DKK">Kroner</option>

options_for_select([ "VISA", "MasterCard" ], "MasterCard")
# => <option value="VISA">VISA</option>
# => <option selected="selected" value="MasterCard">MasterCard</option>

options_for_select({ "Basic" => "$20", "Plus" => "$40" }, "$40")
# => <option value="$20">Basic</option>
# => <option value="$40" selected="selected">Plus</option>

options_for_select([ "VISA", "MasterCard", "Discover" ], ["VISA", "Discover"])
# => <option selected="selected" value="VISA">VISA</option>
# => <option value="MasterCard">MasterCard</option>
# => <option selected="selected" value="Discover">Discover</option>

Booleanの選択肢生成

options_for_select({ "TRUE": true, "FALSE": false }, false)
# => "<option value="true">TRUE</option>\n<option selected="selected" value="false">FALSE</option>"

参考