LoginSignup
3
1

More than 5 years have passed since last update.

Rails で <select> 要素とか <input type=radio> 要素を readonly っぽくする

Last updated at Posted at 2018-05-16
  • 編集不可かつリクエストには含めたい 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 %>
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