form_for FormBuilder

I'm making heavy use of a custom FormBuilder. Im using the common wrapper method name "tagged_form_for" to wrap #form_for. However, this limits me to _surrounding_ the <form/> with my tags (in this case im using a table).

So my resulting output is along the lines of:

<table>   <form>    <tr><td>...stuff...</td></tr>   </form> </table>

This isnt playing very well with firefox and innerHTML (hard to explain, but it makes the form invisible). The solution is to render it more appropriately like so:

<form>   <table>     <tr><td>...stuff...</td></tr>   </table> </form>

The problem I have with that is it appears to do that I have to override the entire #form_for method. That will work _for now_ but Id like to know if anyone has a more elegant solution to inject my elements _after_ the <form> and before the </form> pieces. Without requiring a hack such as <%= f.start_table %> <%= f.end_table %> would be preferable.

If I come up with anything in the meantime I will post it back here as well.

Joe

I'm making heavy use of a custom FormBuilder. Im using the common wrapper method name "tagged_form_for" to wrap #form_for. However, this limits me to _surrounding_ the <form/> with my tags (in this case im using a table).

So my resulting output is along the lines of:

<table>   <form>    <tr><td>...stuff...</td></tr>   </form> </table>

This isnt playing very well with firefox and innerHTML

I think a key problem with this is that it's not valid (X)HTML.

The problem I have with that is it appears to do that I have to override the entire #form_for method. That will work _for now_ but Id like to know if anyone has a more elegant solution to inject my elements _after_ the <form> and before the </form> pieces. Without requiring a hack such as <%= f.start_table %> <%= f.end_table %> would be preferable.

I came across this technique on the web someplace, but I can't remember where. (I know I was reading the "When V is for Vexing" pdf[0] by Bruce Williams and Marcel Molina,Jr., which encourages custom view helpers in general and I googled quite a bit as well.) While this doesn't solve the issue in the way you've described, perhaps it'll point you towards a satisfactory solution. This creates a form helper that automatically adds a label element for each input element.

# helpers/labeled_builder.rb class LabeledBuilder < ActionView::Helpers::FormBuilder    def self.create_labeled_field(method_name)      define_method(%(labeled_#{method_name})) do |label, *args|      @template.content_tag("label",                            label.to_s.humanize,                            :for => %(#{@object_name}_#{label})) +        self.send(method_name.to_sym, label, *args)      end    end

   field_helpers.each do |name|      create_labeled_field(name)    end end

# helpers/builder_helper.rb module BuilderHelper    def labeled_form_for(name, *args, &block)      options = args.last.is_a?(Hash) ? args.pop : { }      options = options.merge(:builder => LabeledBuilder)      args = (args << options)      form_for(name, *args, &block)    end end

I've included BuilderHelper in ApplicationHelper.

I call it like:

<% labeled_form_for :object do |f| %> <%= f.labeled_text_field :attr_1 %> <%= f.labeled_text_area :attr_2 %> <% end %>

I think you could extend this so it would automatically include the table start and end tags. I'm not sure how though :slight_smile:

Hope this helps.

Michael Glaesemann grzm seespotcode net

[0](Codefluency.com)