2011-02-07 88 views
12

我正在使用Rails 2.3.8。我的代碼:在Rails中關聯標籤和單選按鈕

<%= 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相同。我該如何糾正?

謝謝。

回答

35

試試這個

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

我是新來的回報率,我覺得formtastic比較容易的方式來處理表單的意見。你可能想試試看:https://github.com/justinfrench/formtastic – LapinLove404 2011-02-07 13:18:10

0

這爲我工作,我在那裏循環通過計劃:

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