Custom Form Builder


#1

One recipe in the recipes book, Creating a Custom Form Builder, we wrap
each
of the field helpers in FormBuilder, so they include table information.
I’m
trying to include selection drop downs, and the way I think I should do
it
is to redefine the ActionView::Helpers::FormOptionsHelper select method
to
do the same. I get into trouble here.

module ApplicationHelper
class TabularFormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(check_box radio_button hidden_field)).each do
|selector|
src = <<-END_SRC
def #{selector}(field, options = {})
@template.content_tag(“tr” ,
@template.content_tag(“td” , field.to_s.humanize + “:” ) +
@template.content_tag(“td” , super))
end
END_SRC
class_eval src, FILE, LINE
end
end

def tabular_form_for(name, object = nil, options = nil, &proc)
concat("

" , proc.binding)
form_for(name,
object,
(options||{}).merge(:builder => TabularFormBuilder),
&proc)
concat("
" , proc.binding)
end
module ActionView::Helpers::FormOptionsHelper
  alias_method :old_select, :select
  def select(object, method, choices, options = {}, html_options = 

{})
@template.content_tag(“tr”,
@template.content_tag(“td”,
( options[:label] || field.to_s.humanize ) + “:”) +
@template.content_tag(“td”, old_select))
end
end

(I thought I didn’t need to use the class_eval for this, as opposed to
the
TabularFormBuilder itself, which composes many overrides )

end

calling in the partial
<%= f.select :contact_id, @contacts.collect { |p| [ p.name, p.id ]
},
{:include_blank => true } %>

gives me error: undefined local variable or method `field’ for
#<#Class:0x387d8e8:0x387d8a0>

So, what am I doing wrong? How do I override the select method in this
context, or should I wrap its output in tags at some other level?

Thanks!

Jim Mack