株式会社TECH LUCKという会社で代表兼エンジニアをしている齊藤です。
毎回調べているので備忘録として。
以下のような構成になっている。
sample.html.erb
<%= collection_select(対象となるモデル名, paramsに贈りたいモデルのカラム, 選択肢として表示されるモデルのレコード(配列), 選択肢として表示されるモデルのカラム, { prompt: "何もも選択されていない場合に表示される文字", select: 選択済みのレコード }) %>
具体的には以下のように使う。
edit.html.erb
<%= collection_select(:product, :id, @products, :id, :name, { prompt: "商品名を選択してください", select: @product }) %>