10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

promptはrenderで消える

Last updated at Posted at 2019-09-15

編集画面を作る際にpromptが消失した

MVCの流れで呼び出される時

Image from Gyazo

### renderで呼び出す時

Image from Gyazo

## 実際のコード
.main__product__delivery__h2-choice
  = f.collection_select :delivery_method_id, @deliveryMethods, :id, :name, prompt: "---"
  = fa_icon "chevron-down", class: "main__product__delivery__sell-box__icon"

どうやら仕様らしい…?

Select prompt option disappears when validation fails in Rails
https://stackoverflow.com/questions/4060737/select-prompt-option-disappears-when-validation-fails-in-rails
You can achieve the results you want by setting :include_blank to the string you were using for prompt.

とのことなのでpromptからinclude_blankに書き換え

.main__product__delivery__h2-choice
  = f.collection_select :delivery_method_id, @deliveryMethods, :id, :name, include_blank: "---", 
  = fa_icon "chevron-down", class: "main__product__delivery__sell-box__icon"

Image from Gyazo
"---"自体は存在していることが確認できたのであとは"---"が初期値として選択されていればOK!

最終的なコード

RubyonRails:collection_selectにデフォルト値を設定する
https://madogiwa0124.hatenablog.com/entry/2018/05/12/210801

こちらのサイトを参考にして

.main__product__delivery__h2-choice
  = f.collection_select :delivery_method_id, @deliveryMethods, :id, :name, { include_blank: "---", selected:"" }
  = fa_icon "chevron-down", class: "main__product__delivery__sell-box__icon"

Image from Gyazo

結論

prompt: "---"を{ include_blank: "---", selected:"" }に書き換える!

10
2
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
10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?