有没有办法告诉 Rails 不要创建 div.field_with_errors
围绕标签和实际字段,但要创建 div.error
围绕他们两个?
例如。从我的 View 中摘录的表单(用 HAML 编写)
= form_for @user do |f|
%div.clearfix
= f.label :name
%div.input
= f.text_field :name
如果出现错误,我希望根 div 为
div.clearfix.error
并避免 field_with_errors
.我可以这样做吗?作为另一种选择,我可以使用格式来创建 Bootstrap -styled 元素,没有 formtastic 的 css 和 html 类,但带有 bootstrap 的类。在使用formtastic的情况下,我可以用错误字段做一些事情吗?
最佳答案
@flowdelic 的答案似乎是最简单的解决方法。但是,名称不正确。
这可能是由于 Rails/Bootstrap 版本,但这对我有用。
/* Rails scaffold style compatibility */
#error_explanation {
@extend .alert;
@extend .alert-error;
@extend .alert-block; /* optional */
}
.field_with_errors {
@extend .control-group.error
}
关于ruby-on-rails - 自定义 field_with_errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7454682/