Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Created May 15, 2013 03:13
Show Gist options
  • Save jackcallister/5581413 to your computer and use it in GitHub Desktop.
Save jackcallister/5581413 to your computer and use it in GitHub Desktop.
Get Bootstrap and Rails error messages to play nicely together.
<div class="control-group <%= control_group_error(@model, :field_name) %>">
<%= f.label :field_name, :class => "control-label" %>
<div class="controls">
<%= f.text_field :field_name %>
<%= error_help_text(@model, :field_name) %>
</div>
</div>
# Prevent default Rails error mark up.
ActionView::Base.field_error_proc = Proc.new {|html, instance| html }
module FormHelper
def control_group_error(model, field_name)
"error" unless model.errors[field_name].blank?
end
def error_help_text(model, field_name)
"<span class='help-inline'>#{model.errors[field_name].join(", ")}</span>".html_safe unless model.errors[field_name].blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment