Skip to content

Instantly share code, notes, and snippets.

@mec
Last active March 20, 2024 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mec/01d7d5e3fcd56f83d0cc6cbb10edcaa4 to your computer and use it in GitHub Desktop.
Save mec/01d7d5e3fcd56f83d0cc6cbb10edcaa4 to your computer and use it in GitHub Desktop.
Rails default form errors
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
fragment = Nokogiri::HTML.fragment(html_tag)
field = fragment.at('input,select,textarea')
error_message = instance_tag.error_message.join(", ")
html = if field
debugger
field.add_class("is-danger")
html = <<-HTML
#{fragment.to_s}
<p class="help is-danger">#{error_message}</p>
HTML
html
else
html_tag
end
html.html_safe
end
# Source:
https://github.com/rails/rails/blob/7c68c5210cbc245d778daa7958cab73bc74f4669/actionview/lib/action_view/base.rb#L162
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment