Skip to content

Instantly share code, notes, and snippets.

@maxx-coffee
Last active February 12, 2016 02:01
Show Gist options
  • Save maxx-coffee/bb7822e70fe1f8e8d9cd to your computer and use it in GitHub Desktop.
Save maxx-coffee/bb7822e70fe1f8e8d9cd to your computer and use it in GitHub Desktop.
Rails bootstrap inline errors
module FormHelper
def bootstrap_errors_for(attr, form, &block )
errors = form.errors[attr]
error_messages = errors.map{|error| content_tag(:span, error)}
content = block_given? ? capture(&block) : "".html_safe
noko_content = Nokogiri::HTML::DocumentFragment.parse(content)
noko_content.css('.form-group')[0]['class'] = "#{noko_content.css('.form-group')[0]['class']} has-error" if errors.present?
if error_messages.present?
html_errors = content_tag(:div, :class => "errors" ) do
error_messages.join("").html_safe
end
noko_content.css('.form-group').children.first.add_previous_sibling(html_errors)
end
noko_content.to_s.html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment