# Form\_for FormBuilder

**URL:** <https://www.ruby-forum.com/t/form-for-formbuilder/111228>\
**Category:** Rails\
**Created:** [August 4, 2007, 12:42am UTC](https://www.ruby-forum.com/t/form-for-formbuilder/111228 "2007-08-04T00:42:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [August 4, 2007, 12:42am UTC](https://www.ruby-forum.com/t/form-for-formbuilder/111228/1 "2007-08-04T00:42:21Z")

</div>

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 with my tags (in this case  
im using a table).

So my resulting output is along the lines of:

| ...stuff... |

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:

| ...stuff... |

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 and before the 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

---

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [August 4, 2007, 4:07am UTC](https://www.ruby-forum.com/t/form-for-formbuilder/111228/2 "2007-08-04T04:07:36Z")

</div>

On Aug 3, 2007, at 17:41 , Joe N. wrote:

> ...stuff... 
> 
> 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 and before the 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 W. 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 🙂

Hope this helps.

Michael G.  
grzm seespotcode net

[0](http://www.codefluency.com/assets/2007/5/18/VisForVexing.pdf)
