# Custom form builder methods won't render in partial

**URL:** <https://www.ruby-forum.com/t/custom-form-builder-methods-wont-render-in-partial/120450>\
**Category:** Rails\
**Created:** [November 17, 2007, 1:00am UTC](https://www.ruby-forum.com/t/custom-form-builder-methods-wont-render-in-partial/120450 "2007-11-17T01:00:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 17, 2007, 1:00am UTC](https://www.ruby-forum.com/t/custom-form-builder-methods-wont-render-in-partial/120450/1 "2007-11-17T01:00:45Z")

</div>

Methods of custom form builder work if the form is in a view, but  
doesn’t work if the form is in a partial.

My rediced code example looks like this:

# – Builder -----------------------------------------

class CustomBuilder \< ActionView::Helpers::FormBuilder

def drawTextField  
return ‘’  
end  
end

# – View --------------------------------------------

\<% form\_for :thisUser,  
:url =\> {:action =\> :testForm},  
:builder =\> CustomBuilder do |form| %\>

\<%= render(:partial =\> ‘testForm’}) rescue nil %\>

\<% end %\>

# – Partial -----------------------------------------

\<%= form.drawTextField %\>

If I simply move the drawTextField line into the view it works fine.  
Using the partial, the page renders everything but the partial (no  
field). No errors in log.

I poked around a fair bit, but have no more ideas to try.

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 17, 2007, 10:11am UTC](https://www.ruby-forum.com/t/custom-form-builder-methods-wont-render-in-partial/120450/2 "2007-11-17T10:11:48Z")

</div>

On Nov 16, 2007, at 3:59 PM, Greg W. wrote:

> Methods of custom form builder work if the form is in a view, but  
> doesn’t work if the form is in a partial.

Nevermind. I’m an idiot. Obviously the |form| object has to be passed  
into the partial. Duh.

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 17, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/custom-form-builder-methods-wont-render-in-partial/120450/3 "2007-11-17T14:32:21Z")

</div>

Greg,  
I don’t mean to muddy the waters on this question, but I believe that  
you need to have a

\<%= render :partial =\> “form”, :locals =\> { :form =\> form } %\>

type statement to pass the ‘form’ value from the calling view to the  
partial.  
Kathy

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 17, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/custom-form-builder-methods-wont-render-in-partial/120450/4 "2007-11-17T19:40:32Z")

</div>

@Kathy

> \<%= render :partial =\> “form”, :locals =\> { :form =\> form } %\>

Yeah, that’s what I did to solve it.

@Bill

> Your formbuilder methods will be available to  
> partials just as if they were any view"

Hmm, well, there’s a definite correlation to it working or not if I  
pass the the form builder into the partial.

> I didn’t see the original stacktrace you sent. Can you  
> paste it back in?

Below is the original reduced code I tested. The update now is that  
if I add a :locals to the render command to pass the form variable  
thru, it works.

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 17, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/custom-form-builder-methods-wont-render-in-partial/120450/5 "2007-11-17T19:14:49Z")

</div>

I don’t think so. Your formbuilder methods will be available to  
partials just as if they were any view. I use a custom formbuilder in  
almost all my forms and I use a lot of partials due to heavy reliance  
on rjs. Greg, I didn’t see the original stacktrace you sent. Can you  
paste it back in?

-Bill

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 18, 2007, 5:17am UTC](https://www.ruby-forum.com/t/custom-form-builder-methods-wont-render-in-partial/120450/6 "2007-11-18T05:17:19Z")

</div>

Why did you think “form” would be in scope in the partial? The way I  
always handle this is as follows:

\<% form\_for :foo do |f| %\>  
\<%= render :partial =\> ‘bar’, :locals =\> {:f =\> f} %\>  
\<% end %\>

Then in the partial, using any member of the yielded object “f” is  
valid.

Does this help?
