我使用Cocoon gem生成该字段。 这是表单代码:
= simple_form_for studynote, validate: true do |f|
#pericopes
= f.simple_fields_for :pericopes do |pericope|
= render 'pericope_fields', f: pericope
.links
= link_to_add_association t('add_pericope'), f, :pericopes, class: 'new btn-xs'
= f.input :title
= f.input :note, :input_html => { :rows => 20 }
= f.button :submit, class: "btn-primary"
字段部分:
.nested-fields
.input-group.mb-0
= f.input :name,
label: "#{ t('simple_form.labels.pericopes.name') } 1",
placeholder: 'Genesis 1:1-3:21',
autofocus: true,
class: "input-group"
%span(class = "input-group-btn")
= link_to_remove_association '', f, class: 'delete', id: 'delete_pericope'
生成的HTML就像这样
<form novalidate="novalidate" class="simple_form edit_studynote" id="edit_studynote_94" data-client-side-validations="{"html_settings":{"type":"SimpleForm::FormBuilder","error_class":"help-block","error_tag":"span","wrapper_error_class":"has-error","wrapper_tag":"div","wrapper_class":"form-group","wrapper":"vertical_form"},"number_format":{"separator":".","delimiter":","},"validators":{"studynote[pericopes_attributes][][name]":{"presence":[{"message":"moet opgegeven zijn"}]},"studynote[title]":{"presence":[{"message":"moet opgegeven zijn"}]},"studynote[note]":{"presence":[{"message":"moet opgegeven zijn"}]}}}" action="/studynotes/94" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="_method" value="patch" /><input type="hidden" name="authenticity_token" value="NXwvUnWX/TRkVsN/8+sIta3YLlT1MB80MVppFIUzNqG/ex6tC0B2ZGrsxoVwyYMHUmeG/MDXeeh96NuV34XzpQ==" /><div id='pericopes'>
<div class='nested-fields'>
<div class='input-group mb-0'>
<div class="form-group string required studynote_pericopes_name"><label class="control-label string required" for="studynote_pericopes_attributes_0_name"><abbr title="required">*</abbr> pericoop 1</label><input class="form-control string required" autofocus="autofocus" placeholder="Genesis 1:1-3:21" type="text" value="Genesis 2" name="studynote[pericopes_attributes][0][name]" id="studynote_pericopes_attributes_0_name" /></div>
<span class='input-group-btn'>
<input type="hidden" name="studynote[pericopes_attributes][0][_destroy]" id="studynote_pericopes_attributes_0__destroy" value="false" /><a class="delete remove_fields existing" id="delete_pericope" href="#"></a>
</span>
</div>
</div>
<input type="hidden" value="114" name="studynote[pericopes_attributes][0][id]" id="studynote_pericopes_attributes_0_id" /><div class='links'>
<a class="new btn-xs add_fields" data-association="pericope" data-associations="pericopes" data-association-insertion-template="<div class='nested-fields'>
<div class='input-group mb-0'>
<div class="form-group string required studynote_pericopes_name"><label class="control-label string required" for="studynote_pericopes_attributes_new_pericopes_name"><abbr title="required">*</abbr> pericoop 1</label><input class="form-control string required" autofocus="autofocus" placeholder="Genesis 1:1-3:21" type="text" name="studynote[pericopes_attributes][new_pericopes][name]" id="studynote_pericopes_attributes_new_pericopes_name" /></div>
<span class='input-group-btn'>
<input type="hidden" name="studynote[pericopes_attributes][new_pericopes][_destroy]" id="studynote_pericopes_attributes_new_pericopes__destroy" value="false" /><a class="delete remove_fields dynamic" id="delete_pericope" href="#"></a>
</span>
</div>
</div>
" href="#">Voeg nog een pericoop toe</a>
</div>
</div>
据我所知,文本字段和按钮都应该在同一个.input-group
标记内。按钮应该有自己的.input-group-button
范围。 See for documentation:https://getbootstrap.com/docs/4.0/components/input-group/
有什么建议吗?