Skip to content

Instantly share code, notes, and snippets.

@ledermann
Created April 6, 2009 16:41
Show Gist options
  • Save ledermann/90826 to your computer and use it in GitHub Desktop.
Save ledermann/90826 to your computer and use it in GitHub Desktop.
# Rails-FormBuilder for creating forms with <label>-Tags
class LabeledFormBuilder < ActionView::Helpers::FormBuilder
helpers = field_helpers +
%w{date_select datetime_select time_select} +
%w{collection_select select country_select time_zone_select} -
%w{radio_button hidden_field label fields_for} # Don't decorate these
helpers.each do |name|
define_method(name) do |field, *args|
options = args.last.is_a?(Hash) ? args.pop : {}
if args.last.is_a?(Hash)
html_options = options
options = args.pop
else
html_options = {}
end
# fix HTML validation error for date_select and date_time_select
if %w(date_select datetime_select).include?(name)
options[:id] = "#{@object_name}_#{field}_3i"
end
if options.has_key?(:label) || html_options.has_key?(:label)
label_name = options.delete(:label) || html_options.delete(:label)
has_label = !label_name.blank?
else
label_name = nil
has_label = true
end
if has_label
label_tag = label(field, label_name, :class => options.delete(:label_class), :for => options[:id])
else
# human_attribute_name is used
# Plugin required: github.com/iain/i18n_label
end
@template.content_tag(:p, label_tag.to_s + super)
end
end
end
ActionView::Base.default_form_builder = LabeledFormBuilder
- form_for :debt, :url => debts_path do |f|
= f.text :number, :label => 'Overriden label'
= f.datetime_select :due_at
= f.text_area :description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment