Skip to content

Instantly share code, notes, and snippets.

@dpmccabe
Forked from t2/application.rb
Last active August 29, 2015 14:05
Show Gist options
  • Save dpmccabe/9144eb7dcf0d58154f33 to your computer and use it in GitHub Desktop.
Save dpmccabe/9144eb7dcf0d58154f33 to your computer and use it in GitHub Desktop.
add file as initializer
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
html = %(<span class="has-error">#{html_tag}</span>).html_safe
form_fields = %w(textarea input select)
element = Nokogiri::HTML::DocumentFragment.parse(html_tag).css(['label', form_fields].join(', ')).first
if element.node_name.eql?('label')
html = %(<span class='has-error'>#{element}</span>).html_safe
elsif form_fields.include?(element.node_name)
html = %(<span class='has-error'>#{element}<span class='help-block'>#{[instance.error_message].join(', ')}</span></span>).html_safe
end
html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment