Rails 3 Custom FormBuilder Options

I am creating a custom FormBuilder for my application, but I do not understand why I need to pass the towing parameters as follows:

f.text_field :my_model, :my_field 

Here is my builder:

 class AceBuilder < ActionView::Helpers::FormBuilder %w[text_field password_field text_area].each do |method_name| define_method(method_name) do |object_name, method, *args| options = args.extract_options! @template.content_tag(:div, :class => "control-group #{@object.errors[method].length > 0 ? 'error' : ''}") do @template.concat( @template.content_tag(:label, options[:label] || method.to_s.humanize, :class => 'control-label', :for => "#{object_name}_#{method}")) @template.concat( @template.content_tag(:div, :class => "controls") do @template.concat(super(method)) @template.concat( @template.content_tag(:span, @object.errors[method].join, :for => "#{object_name}_#{method}", :class => "help-inline" ) ) end ) end end end 

I know that

 define_method(method_name) do |object_name, method, *args| 

has two parameters, so my question is:

How can I write this FormBuilder in such a way that I can name it as follows:

 f.text_field :my_field 

Thanks.

0
source share
1 answer

It was easy when I figured out how to: replace object_name with @object_name

 class AceBuilder < ActionView::Helpers::FormBuilder %w[text_field password_field text_area].each do |method_name| define_method(method_name) do |method, *args| options = args.extract_options! @template.content_tag(:div, :class => "control-group #{@object.errors[method].length > 0 ? 'error' : ''}") do @template.concat( @template.content_tag(:label, options[:label] || method.to_s.humanize, :class => 'control-label', :for => "#{@object_name}_#{method}")) @template.concat( @template.content_tag(:div, :class => "controls") do @template.concat(super(method)) @template.concat( @template.content_tag(:span, @object.errors[method].join, :for => "#{@object_name}_#{method}", :class => "help-inline" ) ) end ) end end end 
0
source

All Articles