Rails通过div包含错误的字段

fl0*_*00r 6 ruby forms validation ruby-on-rails

如何在未通过验证时停止Rails更改我的代码.

每次铁路包裹我的田地

<div class='field_with_error'>...</div>
Run Code Online (Sandbox Code Playgroud)

我可以编辑fields_with_error课程

.fields_with_error{ display: inline }
Run Code Online (Sandbox Code Playgroud)

这是有效的,但它是hacky

Ris*_*ogi 7

没关系.使用CSS而不是这样做.

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
  "<span class='field_error'>#{html_tag}</span>"
end

我觉得哪个更hacky :)


aur*_*bee 5

我在environment.rb中使用它.更加hacky ;-)

#
# Fix annoying <div class="fieldsWithError"> wrapping after validation
# http://dev.rubyonrails.org/ticket/3587
#

ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| 
  msg = instance.error_message 

  if html_tag =~ /<(input|textarea|select)[>]+class=/
    class_attribute = html_tag =~ /class=['"]/ 
    html_tag.insert(class_attribute + 7, "error ") 
  elsif html_tag =~ /<(input|textarea|select)/
    first_whitespace = html_tag =~ /\s/ 
    html_tag[first_whitespace] = " class='error' "
  end 

  html_tag
}
Run Code Online (Sandbox Code Playgroud)