Skip to content

Instantly share code, notes, and snippets.

@timriley
Created March 30, 2010 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timriley/348707 to your computer and use it in GitHub Desktop.
Save timriley/348707 to your computer and use it in GitHub Desktop.
<% form_for(@article, :builder => SmartLabelFormBuilder) do |form| %>
<% form.label(:title) do %>
Title
<%= form.text_field(:title) %>
<% end %>
<% end %>
class SmartLabelFormBuilder < ActionView::Helpers::FormBuilder
def label(method, content_or_options_with_block = nil, options = {}, &block)
if !block_given?
# No block, use the standard label helper.
super(method, content_or_options_with_block, options)
else
# We've got a block. This is where we want to do custom shit.
options = content_or_options_with_block.is_a?(Hash) ? content_or_options_with_block.stringify_keys : {}
if errors_on?(method)
(options['class'] = options['class'].to_s + ' error').strip!
end
@template.content_tag(:label, options, &block)
end
end
private
def errors_on?(method)
@object.respond_to?(:errors) && @object.errors.respond_to?(:on) && @object.errors.on(method.to_sym)
end
end
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
html_tag
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment