LoginSignup
1
0

More than 3 years have passed since last update.

f.collection_selectの使い方(超ざっくり&初学者向け)

Posted at

f.collection_selectで別モデルのカラムを使って選択肢を作りたい時に役立つ情報を提供します!

まずf.collection_selectについて
書き方としては

<%= f.collection_select :保存先カラム, :選択肢に使いたいカラムを持った配列, :保存先カラムに保存したい値, :選択肢に表示したいカラム %>といった感じです。

例としては<%= f.collection_select :genre_id, @genres, :id, :name, :prompt => "選択してください" %>こんな感じです。

promptに関しては未選択の時に表示させておきたい文字を表示させることができます!!

ぜひ使ってみてください!!

1
0
1

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
1
0