Last Updated: February 25, 2016
·
533
· rxgx

Unobtrusive Form Errors in Rails

Let's just add an error class to the LABEL and INPUT or TEXTAREA fields instead of injecting layers (<div class="error-fields">) into the form view.

config.action_view.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