运行循环时无法使用 ruby on rails 中的标签选择单选按钮? [英] Unable to select radio buttons using labels in ruby on rails while running through a loop?
本文介绍了运行循环时无法使用 ruby on rails 中的标签选择单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我为 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 %>
这篇关于运行循环时无法使用 ruby on rails 中的标签选择单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文