Forum: Ruby on Rails Custom Form Builder

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jim mack (Guest)
on 2006-05-20 09:15
(Received via mailing list)
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("<table>" , proc.binding)
    form_for(name,
             object,
     (options||{}).merge(:builder => TabularFormBuilder),
    &proc)
    concat("</table>" , 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
This topic is locked and can not be replied to.