LoginSignup
0
0

More than 1 year has passed since last update.

options_from_collection_for_selectですでに選択されている値を呼び出す

Posted at

form_forヘルパーでselectを利用する際に、options_from_collection_for_selectを利用した。その際、すでに保存されている値を呼び出す方法につまづいたので、メモを残しておく。

NG例。selectedで指定しようとしても取れない。

  = f.select :id, options_from_collection_for_select(Users.all, :id, :username), {include_blank: true}, class: "form-control", selected: user.id

OK例。options_from_collection_for_selectの第三引数に指定してあげればOK

  = f.select :user_id, options_from_collection_for_select(Users.all, :id, :username, user.id), {include_blank: true}, class: "form-control"
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