这是我为 RAILS 中的 RADIO BUTTON 编写的代码
Here is the code I am writing for RADIO BUTTON in RAILS
<% @Days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] %>
<% @Days.each do |day| %>
<%= f.radio_button :due_day, day, :checked => @group.due_day.eql?(day) %>
<%= f.label :due_day, day %>
<% end %>
我在 HTML get
<input checked="checked" id="group_due_day_monday" name="group[due_day]" type="radio" value="Monday" />
<label for="group_due_day">Monday</label>
.....
.....
<input id="group_due_day_sunday" name="group[due_day]" type="radio" value="Sunday" />
<label for="group_due_day">Sunday</label>
但我希望它喜欢
<input checked="checked" id="group_due_day_monday" name="group[due_day]" type="radio" value="Monday" />
<label for="group_due_day_monday">Monday</label>
.....
.....
<input id="group_due_day_sunday" name="group[due_day]" type="radio" value="Sunday" />
<label for="group_due_day_sunday">Sunday</label>
这样如果标签被点击,RADIO BUTTON就会被选中.这可以使用
So that if the label is clicked RADIO BUTTON gets selected. This can be achieved using
<%= f.radio_button :due_day, day, :checked => @group.due_day.eql?(day) %>
<%= f.label :due_day_monday, day %>
但如果我有一组 USERS 说 @users,这不是一个好的做法.我不知道 array 的 length 以及 用户名 ??
But this is not good practice in case, if I have a set of USERS says @users. Where I dont know the length of the array and also the users names ??
FormHelper label
在这种情况下正好接受 :value
选项:
<% @Days.each do |day| %>
<%= f.radio_button :due_day, day, :checked => @group.due_day.eql?(day) %>
<%= f.label :due_day, day, :value => day %>
<% end %>