我正在使用 Bootstrap 3 和 Rails 4,我想创建一个自定义 FormBuilder 来处理一些 Bootstrap 独特的 HTML 语法。具体来说,我需要一个自定义帮助程序来围绕表单字段创建 form-group
div 包装器,因为 Bootstrap applies error state to this wrapper, and not the field itself ...
<div class="form-group has-error">
<label class="col-md-3 control-label" for="user_email">Email</label>
<div class='col-md-9'>
<input class="form-control required-input" id="user_email" name="user[email]" placeholder="peter@example.com" type="email" value="someone@example.com" />
</div>
</div>
注意外层 div 中的额外类 has-error
...
无论如何,我写了那个助手,而且效果很好!
def form_group(method, options={})
class_def = 'form-group'
class_def << ' has-error' unless @object.errors[method].blank?
class_def << " #{options[:class]}" if options[:class].present?
options[:class] = class_def
@template.content_tag(:div, options) { yield }
end
# Here's a HAML sample...
= f.form_group :email do
= f.label :email, nil, class: 'col-md-3 control-label'
.col-md-9
= f.email_field :email, class: 'form-control required-input', placeholder: t('sample.email')
现在我想利用 Bootstrap 的 form help text为了显示错误信息。这需要我扩展 Rails native 助手(例如上面示例中的 text_field
),然后在 f.form_group
的 block 内调用它们。
解决方案似乎足够简单:调用父级,并将我的 span block 附加到末尾...
def text_field(method, options={})
@template.text_field(method, options)
if !@object.errors[method].blank?
@template.content_tag(:span, @object.errors.full_messages_for(method), class: 'help-block')
end
end
只是它不会输出任何 HTML,div 只会显示为空。我尝试了一堆不同的语法方法:
super
对比text_field
对比text_field_tag
concat
-ing 结果 --@template.concat(@template.content_tag( [...] ))
- 动态变量,例如
def text_field(method, *args)
然后是options = args.extract_options!.symbolize_keys!
我只会遇到奇怪的语法错误,或者一个空的 div
。在某些情况下,input
字段会出现,但帮助文本 span
不会出现,反之亦然。
我确定我搞砸了一些简单的事情,只是我没有看到。
最佳答案
花了几天时间,但我最终偶然发现了正确的语法。希望它能拯救其他人的理智!
Ruby 的return
automagic,结合 Rails 有时复杂的作用域,让我失去了平衡。具体来说,@template.text_field
绘制内容,但它必须由辅助方法返回才能出现在调用 block 内。但是我们必须返回两次调用的结果...
def text_field(method, options={})
field_errors = object.errors[method].join(', ') if !@object.errors[method].blank?
content = super
content << (@template.content_tag(:span, @object.errors.full_messages_for(method), class: 'help-block') if field_errors)
return content
end
我们必须返回父方法(通过 super
)加上我们自定义的 @template.content_tag(:span,
注入(inject)的结果。我们可以将其缩短为使用 Ruby 的加号 +
运算符连接返回结果。
def text_field(method, options={})
field_errors = object.errors[method].join(', ') if !@object.errors[method].blank?
super + (@template.content_tag(:span, @object.errors.full_messages_for(method), class: 'help-block') if field_errors)
end
注意:表单是使用 ActiveModel 对象启动的,这就是我们可以访问 @object
的原因。实现 form_for
而不将其与模型关联将需要您改为扩展 text_field_tag
。
这是我完成的自定义 FormBuilder
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
def form_group(method, options={})
class_def = 'form-group'
class_def << ' has-error' unless @object.errors[method].blank?
class_def << " #{options[:class]}" if options[:class].present?
options[:class] = class_def
@template.content_tag(:div, options) { yield }
end
def text_field(method, options={})
field_errors = object.errors[method].join(', ') if !@object.errors[method].blank?
super + (@template.content_tag(:span, @object.errors.full_messages_for(method), class: 'help-block') if field_errors)
end
end
不要忘记告诉 form_for
!
form_for(:user, :builder => BootstrapFormBuilder [, ...])
编辑:这里有一些有用的链接,它们帮助我走上了启蒙之路。 Link-juice 感谢作者!
- Writing a custom FormBuilder in Rails 4.0.x
- Formatting Rails Errors for Twitter Bootstrap
- Very Custom Form Builders in Rails
- SO: Nesting content tags in rails
- SO: Rails nested content_tag
- SO: Rails 3 Custom FormBuilder Parameters
- SO: Trying to extend ActionView::Helpers::FormBuilder
- RailsGuides: Form Helpers
- Real world sample from treebook tutorial code
关于ruby-on-rails - 如何扩展核心 Rails FormBuilder 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22183500/