错误(div class =“fieldWithErrors”)解决方法?

在我的项目中,我有一个注册表。

我的HTML看起来像

现在通过提交空表单生成错误我的代码如下:

  

这将破坏我的布局。 有没有解决方案呢

  

代替

 

创建一个新的初始化程序,例如/config/initializers/field_error.rb

 ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag| if html_tag =~ /<(input|textarea|select)[^>]+class=/ class_attribute = html_tag =~ /class=['"]/ html_tag.insert(class_attribute + 7, "fieldWithErrors ") elsif html_tag =~ /<(input|textarea|select)/ first_whitespace = html_tag =~ /\s/ html_tag[first_whitespace] = " class='fieldWithErrors' " end html_tag end 
  • Ryan Bates在Railscast第39集中介绍了这一点

这是Rails如何显示表单错误的错误。 这是Rails的一个古老而已知的问题: https : //rails.lighthouseapp.com/projects/8994/tickets/1626-fieldwitherrors-shouldnt-use-a-div

为什么它还没有解决对我来说是一个谜。 我想这是你需要在Rails中手动覆盖的许多小东西之一。 但绝对可以尝试John Topley的解决方法。 或者将其修复到本地Rails代码中并将其作为补丁提交给Rails。

也许你应该考虑使用formtastic http://github.com/justinfrench/formtastic

Railscast是要走的路,但你需要为Rails 3.x更新它。

将此添加到environment.rb的末尾

 ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag| "#{html_tag}".html_safe end 

退回rails应用程序,您将被设置。