Skip to content

Instantly share code, notes, and snippets.

@trotter
Forked from r38y/labeled_form_builder.rb
Created February 27, 2010 19:31
Show Gist options
  • Save trotter/316897 to your computer and use it in GitHub Desktop.
Save trotter/316897 to your computer and use it in GitHub Desktop.
class LabeledFormBuilder < ActionView::Helpers::FormBuilder
%w[text_field password_field select file_field text_area].each do |method_name|
alias_method("#{method_name}_without_randys_awesome", method_name)
define_method(method_name) do |field_name, *args|
content = ''
error = object.errors.on(field_name)
row_classes = 'form-row'
row_classes += ' error' unless error.blank?
label_classes = 'form-label'
label_classes += ' align' if %w|file_field select|.include?(method_name)
content += @template.content_tag(:div, :class => row_classes) do
inner = @template.content_tag(:div, :class => label_classes) do
field_label(field_name, *args)
end
inner += @template.content_tag(:div, :class => 'form-field') do
# Error: super called outside of method
send("#{method_name}_without_randys_awesome", field_name, *args) + modifier_text(*args)
end
inner += form_comment(*args)
inner += field_error(field_name)
inner
end
content
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment