在Rails中关联标签和单选按钮

时间:2011-02-07 12:29:26

标签: ruby-on-rails forms

我正在使用Rails 2.3.8。我的代码:

<%= f.radio_button :status, "draft" %>
<%= f.label :status, "Draft" %>
<%= f.radio_button :status, "published" %>
<%= f.label :status, "Published" %>

输出:

<input id="shop_status_draft" name="shop[status]" type="radio" value="draft" />
<label for="shop_status">Draft</label>
<input checked="checked" id="shop_status_published" name="shop[status]" type="radio" value="published" />
<label for="shop_status">Published</label>

显然,label未正确关联我的单选按钮。我想将label与单选按钮id相同。我该如何纠正?

感谢。

2 个答案:

答案 0 :(得分:35)

试试这个

<%= f.radio_button :status, "draft" %>
<%= f.label :status, "Draft", :value => "draft" %>
<%= f.radio_button :status, "published" %>
<%= f.label :status, "Published", :value => "published" %>

答案 1 :(得分:0)

这对我有用,我正在制定计划:

<% @plans.each do |plan| %>
    <%= radio_button_tag :plan_id, plan.id %> 
    <%= label_tag 'plan_id_' + plan.id.to_s, plan.name %>
<% end %>