# Custom FormBuilder problems

**URL:** <https://www.ruby-forum.com/t/custom-formbuilder-problems/126154>\
**Category:** Rails\
**Created:** [January 15, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/custom-formbuilder-problems/126154 "2008-01-15T21:29:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [January 15, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/custom-formbuilder-problems/126154/1 "2008-01-15T21:29:55Z")

</div>

I’m trying to create my first custom FormBuilder and I’m having a bit  
of a problem. I’m trying to add a ‘field\_group’ method to my builder  
that takes a block, but when I yield to the block I seem to be getting  
duplicate form field entries being produced. Maybe my code will make  
this clearer:

class LabelledBuilder \< ActionView::Helpers::FormBuilder  
def self.define\_labelled\_field\_method(method\_name)  
src = \<\<-end\_src  
def #{method\_name}(label, method, options = {})  
hint\_text = options[:hint]  
@template.content\_tag(“tr”) do  
@template.content\_tag(“td”,  
@template.content\_tag(“label”,  
label.to\_s.humanize.titleize, :for =\> method)) +  
@template.content\_tag(“td”, super(method, options) +  
(hint\_text.nil? ? ‘’ : @template.content\_tag(“div”,  
hint\_text, :class =\> “field\_hint”)))  
end  
end  
end\_src  
class\_eval src, **FILE** , **LINE**  
end

def check\_box(label, method, options = {})  
super(method, options) +  
@template.content\_tag(“label”, label, :for =\> method)  
end

def field\_group(label, &proc)  
raise ArgumentError, “Missing block” unless block\_given?  
@template.concat(yield, proc.binding) # \<— !!! PROBLEM  
SEEMS TO BE CAUSED BY THIS YIELD !!!  
end

(field\_helpers - %w(check\_box)).each do |name|  
define\_labelled\_field\_method(name)  
end  
end

If I put this in my view

\<% form\_for :user, :url =\> {:controller =\> :user, :action  
=\> :new }, :builder =\> LabelledBuilder do |f| %\>

 \<%= f.text\_field "user name:", :user\_name, :hint =\> "Enter user name" %\> \<% f.field\_group "includes:" do %\> \<%= f.check\_box "box", :terms\_of\_service %\> \<% end %\> 

\<% end %\>

the text field and the checkbox are emitted correctly, but then I also  
get an inner element embedded in the outer element, and  
the two fields are repeated. Something like this:

```
<input id="user_terms_of_service" name="user[terms_of_service]"

```

type=“checkbox” value=“1” /\>box

| User Name: | 
Enter user name
 |

```
<input id="user_terms_of_service" name="user[terms_of_service]"

```

type=“checkbox” value=“1” /\>box

| User Name: | 
Enter user name
 |

I believe it has something to do with the call to ‘yield’ in my  
field\_group method, but I’m not sure why this is happening. Can  
anybody explain this to me?

TIA,  
– bb

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [January 15, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/custom-formbuilder-problems/126154/2 "2008-01-15T22:25:49Z")

</div>

I think I figured it out. The problem was related to the fact that I  
was calling ‘yield’ within a call to @template.concat, and the block  
contained \<%= %\> blocks, so the erb blocks were omitting the fields  
and so was the call to @template.concat. Seems to be working now.

Thanks anyway,  
– bb
