Last Updated: February 24, 2018
·
22.19K
· anthonator

Remove Rails field_with_errors wrapper

Are your form styles screwed up after validation? Are you sick of dealing with the standard Rails field_with_errors wrappers?

GET RID OF THEM!!!!11!

Here's a simple trick to do away with those pesky wrappers once and for all. Just add this block to your config/environment.rb file.

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  html_tag.html_safe
end

Problem solved.

6 Responses
Add your response

Sometimes I wish more Rails features were opt-in - but then I'd spend a week customizing each project ><

over 1 year ago ·

I love you! <3

over 1 year ago ·

There's no reason to do this. If you don't want field_with_errors styled specially, just ignore it. If the extra <div> elements break your layout, just put something like the following in your CSS: .field_with_errors { display: inline }.

over 1 year ago ·

@marnen when there is no indication of the div in the code, I bet the given solution is the best you can go with

over 1 year ago ·

easy and efficient. Thanks !

over 1 year ago ·

Thanks a lot

over 1 year ago ·