Those damn field with errors divs

Posted by gabe-fuzz

September 25 2009 at 9:42am

It seems to be a common issue where css developers are battling with the <div> tags that rails drops in when there are errors in a form. In searching for a solution to this I came across this one:

ActionView::Base.field_error_proc = Proc.new { |html_tag, instance|
"<span class=\"fieldWithErrors\">#{html_tag}</span>" }

placed at the end of your config/environment.rb file. This appeared to work fine until I tried firing up my console getting the following error:

	Loading development environment (Rails 2.3.3)
	/config/environment.rb:45:NameError: uninitialized constant ActionView
	/Library/Ruby/Gems/1.8/gems/rails-2.3.3/lib/rails/backtrace_cleaner.rb:2:NameError: uninitialized constant ActiveSupport::BacktraceCleaner
	/Library/Ruby/Gems/1.8/gems/rails-2.3.3/lib/console_with_helpers.rb:5:NameError: uninitialized constant ApplicationController

… so console doesn’t know anything about ActionView and thusly fails to load your environment when it crosses that line. So instead I decided to move the code to config/initializers/field_with_errors.rb like so:


module ActionView
class Base
def self.field_error_proc
Proc.new { |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" }
end
end
end

Worked like a charm.

Twitter Updates

    via Twitter

    Categories

    Flickr