LoginSignup
3
1

More than 5 years have passed since last update.

validate時にselectボックスで値を保持する

Posted at

値を保持する方法

validateがかかった時にselectボックスからユーザーが選択していた値を保持するには
基本的には下記のような記述でOK

= f.select :fruit, options_for_select([りんご,なし], selected: ticket.fruit), class: "input-item",:prompt => "開始時間"

ただ、時間などは値が変換されて返ってくるので上記のように書いても値が適用されない。
strftimeで変換して上げる必要がある。
そのままだと初期状態がnilでエラーになるのでtryをつけるとうまくいきます。

= f.select :event_time, options_for_select([12:00,13:00], selected: ticket.event_time.try(:strftime,'%H:%M')), class: "input-item",:prompt => "開始時間"

参考記事

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