select_tag
ドキュメント
選択ボックスを生成
options_for_select
ドキュメント
配列・ハッシュから選択肢を生成する
今回作成したいHTML
<select name="select" id="select">
<option selected="selected" disabled="disabled" value="blank">選択 ▼</option>
<option disabled="disabled" value="value1">key1</option>
<option disabled="disabled" value="value2">key2</option>
</select>
⇦選択ボックスがあって
⇦選択肢は見れるけど選択はできない
別に全部選択できないようにする必要がないって場合でも
ちょっと手を加えれば一部だけ選択ができないとかでもできる。
slimで書く
erbでもいいんだけど最近slimで書く機会が多くてね...
- choices = [['選択 ▼','blank'],['key1','value1'],['key2','value2']]
= select_tag :select,
options_for_select(choices, selected: 'blank', disabled: choices.map(&:last))
ドキュメントには:selected
が指定できるのは書いてあるけど
:disabled
が指定できるのは書いてない。
disabled="disabled"
にしたい項目のvalueを配列で指定すると
その項目は選択することができなくなる。
ちなみに
- choices = [['選択 ▼','blank'],['key1','value1'],['key2','value2']]
= select_tag :select,
options_for_select(choices, selected: 'blank'),
disabled: true
とすると
セレクトボックスそのものが無効になり、選択肢を確認することすら不可能になる。
(厳密には開発者ツール使えば見れるけど)
終わり。