如果未通过验证,如何阻止Rails更改我的代码。
每当rails用
包裹我的场地时<div class='field_with_error'>...</div>
我可以修改fields_with_error
类
.fields_with_error{ display: inline }
哪个有效,但它很hacky
答案 0 :(得分:7)
很好。使用CSS而不是这样做。
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag| "<span class='field_error'>#{html_tag}</span>" end
我觉得更讨厌:)
答案 1 :(得分: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
}
答案 2 :(得分:-1)
您也可以使用jQuery来完成它。仍然感觉像是黑客,但它的工作。
$('.field_with_errors input').unwrap();