LoginSignup
1
3

More than 3 years have passed since last update.

プルダウンメニューの項目をDBのカラムから作成する方法

Last updated at Posted at 2019-07-24

やりたいこと

今回は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での表示は以下の通り

1.通常時
スクリーンショット 2019-05-01 16.36.39.png

2.プルダウンメニュークリック時
image.png

オプションについてはまだ今度で・・・

参照先:
https://qiita.com/colorrabbit/items/b58888506e41d1370fd1

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