2014-09-04 45 views
0

我有以下代码:simple_form添加标签集合单选按钮

<%= f.collection_radio_buttons :access_type_id, AccessType.all, :id, :name, group_label_method: 'Access type' %> 

,这让我:使用以下HTML

enter image description here

<fieldset> 
... 
    <span> 
    <label for="log_file_access_type_id_1" name="log_file[access_type_id]"> 
     <input id="log_file_access_type_id_1" name="log_file[access_type_id]" type="radio" value="1"> 
     <label class="collection_radio_buttons" for="log_file_access_type_id_1"> 
     public 
     </label> 
    </label> 
    </span> 
    <span> 
    <label for="log_file_access_type_id_2" name="log_file[access_type_id]"> 
     <input id="log_file_access_type_id_2" name="log_file[access_type_id]" type="radio" value="2"> 
     <label class="collection_radio_buttons" for="log_file_access_type_id_2"> 
     protected 
     </label> 
    </label> 
    </span> 
    <span> 
    <label for="log_file_access_type_id_3" name="log_file[access_type_id]"> 
     <input id="log_file_access_type_id_3" name="log_file[access_type_id]" type="radio" value="3"> 
     <label class="collection_radio_buttons" for="log_file_access_type_id_3"> 
     private 
     </label> 
    </label> 
    </span> 
... 
</fieldset> 

我想要为单选按钮组添加label。我尝试使用group_label_method: 'Access type',但没有任何改变。

有没有办法使用simple_form方法添加这样的标签,或者我应该只添加为纯HTML?

回答

2

试试这个

简单的形式http://rubydoc.info/github/plataformatec/simple_form/SimpleForm/FormBuilder:collection_radio_buttons

collection_radio_buttons(:access_type_id, AccessType.all, :id, :name_with_initial) do |b| 
    b.label(:"data-value" => b.value) { b.radio_button + b.text } 
end 
+0

我得到'未定义的方法'name_with_initial”为#<接入类型:0x00000006298f28>' – gotqn 2014-09-04 11:45:02

+0

@gotqn那是因为这不是可调用的方法你' AccessType'模型。你可以使用':name'。这段代码是从'collection_radio_buttons'的文档中复制而来的。 – engineersmnky 2014-09-04 13:21:55

+0

你把这个放在haml中? – Ashbury 2015-06-07 10:25:04