- 編集不可かつリクエストには含めたい
input type=text
要素を使いたい時、readonly
属性を設定する - しかし
readonly
は<select>
とか<input type="radio">
には効かない。-
disabled
にした入力要素はその form でのリクエストに含まれなくかる。なので選択中の値以外をdisabled
にするなどの工夫が必要
-
こんな感じ:
some_form.html
<select name="some_selection">
<option selected>one</option>
<option disabled>two</option>
</select>
<label><input type="radio" name="some_radio" value="one" checked>one</label>
<label><input type="radio" name="some_radio" value="two" disabled>two</label>
Rails ではこんな感じ:
some_form.html.erb
<%= form_for @some_model do |f| %>
<%= f.select :some_selection,
options_for_select([[:one, selected: true], [:two, disabled: true]]) %>
<label><%= f.radio_button :some_radio, :one, checked: true %></label>
<label><%= f.radio_button :some_radio, :two, disabled: true %></label>
<% end %>