Skip to content

Instantly share code, notes, and snippets.

View dpmccabe's full-sized avatar

Devin McCabe dpmccabe

  • Broad Institute of MIT and Harvard
  • Cambridge, MA
View GitHub Profile
@dpmccabe
dpmccabe / field_with_error.rb
Last active August 29, 2015 14:05 — forked from t2/application.rb
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