if Rails.env == 'development'
  module ActionView::Helpers::FormHelper
    module WithDevelopmentErrors
      def form_for(*args, **kw_args, &block)
        super(*args, **kw_args) do |form|
          append_errors_to_form(form, &block)
        end
      end

      def form_with(*args, **kw_args, &block)
        super(*args, **kw_args) do |form|
          append_errors_to_form(form, &block)
        end
      end

      private

      def append_errors_to_form(form, &block)
        html = ''.html_safe
        if form.object && form.object.respond_to?(:errors) && form.object.errors.present?
          html << content_tag(:div, form.object.errors.full_messages.collect { |m| h m }.join('<br />').html_safe,
            :class => 'development_errors', :onclick => 'this.parentNode.removeChild(this);')
          html << '<style type="text/css"><!--'.html_safe
          css = <<-EOF
            .development_errors {
              position: fixed;
              bottom: 0;
              right: 0;
              z-index: 999999;
              font-size: 11px;
              line-height: 15px;
              background-color: #fed;
              border-top: 1px solid #cba;
              border-left: 1px solid #cba;
              color: #821;
              padding: 10px;
              cursor: pointer;
              opacity: 0.8;            }
          EOF
          html << css.html_safe
          html << '</style>'.html_safe
        end
        html << capture(form, &block)
        html
      end
    end

    prepend WithDevelopmentErrors

  end
end