Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this?
You should override ActionView::Base.field_error_proc
. It's currently defined as this within ActionView::Base
:
@@field_error_proc = Proc.new{ |html_tag, instance|
"<div class=\"field_with_errors\">#{html_tag}</div>".html_safe
}
You can override it by putting this in your application's class inside config/application.rb
:
config.action_view.field_error_proc = Proc.new { |html_tag, instance|
html_tag
}
Restart rails server for this change to take effect.
The visual difference you are seeing is happening because the div
element is a block element. Add this style to your CSS file to make it behave like an inline element:
.field_with_errors { display: inline; }
I currently use this solution, placed in an initializer:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
class_attr_index = html_tag.index 'class="'
if class_attr_index
html_tag.insert class_attr_index+7, 'error '
else
html_tag.insert html_tag.index('>'), ' class="error"'
end
end
This allows me to merely add a class name to the appropriate tag, without creating additional elements.