Re: Help with custom FormBuilder

Joshua Muheim
2007-03-21T21:06:56Z

I''ve got another question...

    def self.create_tagged_field(method_name, options = {})
      define_method(method_name) do |label, *args|
        html_surround(label, super, options) # Provides the HTML code
that surrounds the form label and the method (super) that generates the
input tag(s)
      end
    end

    field_helpers.reject{|h| h ==
''hidden_field''}.concat([''has_and_belongs_to_many'',
''belongs_to'']).each
do |name|
      label_clickable = true unless name ==
''has_and_belongs_to_many''
      create_tagged_field(name, :label_clickable => label_clickable)
    end

How do I find out in define_method part what id attribute the created
form field method will get?

So far I''m guessing it myself in html_surround:

caption_options[:for] = "#{@object_name}_#{label}"

But sometimes this is not correct. Using a text_field it generates

member_nickname

which is fine but in other cases - e.g. a collection_select - it
generates

member_origin_country

which is often incorrect because the ID depends there on the association
name itself. In my case it should be member_country_id.

So I guess there''s a way to examine the ID that Rails will assign to
the
form element --- but how? :-)

Thanks
Josh