Last active
February 12, 2016 02:01
-
-
Save maxx-coffee/bb7822e70fe1f8e8d9cd to your computer and use it in GitHub Desktop.
Rails bootstrap inline errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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