0
0

More than 1 year has passed since last update.

Railsのcollection_selectの使い方

Last updated at Posted at 2023-01-06

株式会社TECH LUCKという会社で代表兼エンジニアをしている齊藤です。

毎回調べているので備忘録として。

以下のような構成になっている。

sample.html.erb
<%= collection_select(対象となるモデル名, paramsに贈りたいモデルのカラム, 選択肢として表示されるモデルのレコード(配列), 選択肢として表示されるモデルのカラム, { prompt: "何もも選択されていない場合に表示される文字", select: 選択済みのレコード }) %>

具体的には以下のように使う。

edit.html.erb
<%= collection_select(:product, :id, @products, :id, :name, { prompt: "商品名を選択してください", select: @product }) %>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0