[Rails]去除错误产生时输入框的显示效果

Rails中当错误发生时,会用红线将出错的入力框框起来,突出显示,如果不需要这种显示效果,可以将其去掉.在/controllers/application.rb中加入如下代码:

ActionView::Base.field_error_proc = Proc.new{|snippet,instance|"#{snippet}"}

如果想更改显示样式,也以同样的方法进行修改,比如下面的代码,将出错的输入框的背景色改为银色,以突出显示.

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
msg = instance.error_message
error_style = "background-color: #ececec"
if html_tag =~ /<(input|textarea|select)[^>]+style=/
style_attribute = html_tag =~ /style=['"]/
html_tag.insert(style_attribute + 7, "#{error_style}; ")
elsif html_tag =~ /<(input|textarea|select)/
first_whitespace = html_tag =~ /\s/
html_tag[first_whitespace] = " style='#{error_style}' "
end
html_tag
end