やりたいこと
今回はUser TBLから、カラムdepが「sales」であるユーザーの名前を、
プルダウンメニューに追加する処理を実装する。
User TBL
id | name | dep |
---|---|---|
1 | takanosuke | sales |
2 | kounosuke | sales |
3 | seinosuke | hr |
collection_selectを利用
<%= form_for @user do |f| %>
<%= f.label :name %>
<%= f.collection_select :name, User.where(dep: "sales"), :name, :name %>
<% end %>
collection_selectの文法は以下の通り
<%= f.collection_select <属性名>, <プルダウンメニュー表示用の配列データ>, <valueとして扱うカラム名>, <表示用のカラム名>, <オプション> %>
viewでの表示は以下の通り
オプションについてはまだ今度で・・・
参照先:
https://qiita.com/colorrabbit/items/b58888506e41d1370fd1