LoginSignup
1
2

More than 5 years have passed since last update.

(.erb: form) ベストプラクティス

Last updated at Posted at 2016-09-09

ラジオボタン

_form.html.erb

<% # 実際にはyamlなどで定義
hash = {
  "1": '男性',
  "2": '女性'
}
%>

<input type="radio" name="sex"> なし # 必要の場合のみ
<% hash.each do |key, value| %>
 <label>
   <input type="radio" name="sex" <%= "checked='checked'" if @user.sex == key.to_s.to_i %> > <%= value %>
 </label>
<% end %>

チェックボックス

_form.html.erb

<% # 実際にはyamlなどで定義
 hash = {
   "1": '水泳',
   "2": 'ランニング'
   "3": 'ウォーキング'
 }
%>

<% hash.each do |id, value| %>
  <label>
    <input name="sports" type="checkbox" value='<%= id %>' <%= "checked='checked'" if @user.sports.include?(id.to_s.to_i) %> <%= value %>
  </label>
<% end %>

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