S-gw
November 17, 2007, 1:00am
1
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)
S-gw
November 17, 2007, 10:11am
2
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)
S-gw
November 17, 2007, 2:32pm
3
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
S-gw
November 17, 2007, 7:40pm
4
@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)
S-gw
November 17, 2007, 7:14pm
5
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
S-gw
November 18, 2007, 5:17am
6
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?