我正在尝试创建一个 rails FormBuilder 以使用引导样式设置表单样式:
<div class="form-group has-error">
<label for="exampleInputEmail1">Email address</label>
<div class="input-group>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
<span class="input-group-addon glyphicon glyphicon-user"></span>
</div>
<p class="help-block>can't be blank</p>
</div>
当我只有一个 div 标签时,我能够让表单构建器工作(不包括传统引导表单的输入组。我的问题是我无法获得带有类“输入组”的 div 的嵌套 content_tag " 正常工作。我尝试添加元素并将 content_tag 包装在捕获中,但无济于事。
class LargeFormBuilder < ActionView::Helpers::FormBuilder
include ActionView::Helpers::TagHelper
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::TextHelper
attr_accessor :output_buffer
%w(text_field text_area email_field password_field).each do |form_method|
define_method(form_method) do |*args|
attribute = args[0]
options = args[1] || {}
options[:label] ||= attribute
options[:class] ||= "form-control input-lg"
label_text ||= options.delete(:label).to_s.titleize
content_tag(:div, class: "form-group #{'has-error' if !object.errors[attribute].empty?}") do
concat label(attribute, label_text, class: "control-label")
concat (
content_tag(:div, class: "input-group") do
concat super(attribute, options)
concat content_tag(:span, "", class: "input-group-addon glyphicon glyphicon-user")
end
)
concat errors_for_field(attribute)
end
end
end
def errors_for_field(attribute, options={})
return "" if object.errors[attribute].empty?
content_tag(:p, object.errors[attribute].to_sentence.capitalize, class: "help-block")
end
end