# Form helpers don't seem to work with RJS files

**URL:** <https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314>\
**Category:** Rails\
**Created:** [May 28, 2010, 4:25pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314 "2010-05-28T16:25:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 28, 2010, 4:25pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/1 "2010-05-28T16:25:15Z")

</div>

Iâ€™ve been spending a great deal of time chasing this problem. Hopefully  
someone sees something obvious Iâ€™m doing wrong. Basically anytime I try  
to call fields\_for from an RJS file or within a partial in the RJS file,  
I get the following error. My goal is simply to load a partial into a  
div. The fields in this partial require fields\_for though. And thatâ€™s  
where the problem lies.

ActionView::TemplateError (undefined method `fields\_for’ for  
“#ActionView::Helpers::FormBuilder:0x944ea28”:String) on line #3 of  
app/views/trials/observe.rjs:  
1: page.alert(‘before’)  
2: test\_func = pluralize(6, ‘cat’)  
3: @form.fields\_for :org\_detail do |w|  
4: page[‘section\_two’].replace\_html :partial =\> “mypartial”,  
:locals =\> { :f =\> w }  
5: end  
6: page.alert(‘after’)

As you can see, the pluralize method seems to work fine, but fields\_for  
always returns this exception. If I run the fields\_for code directly in  
my main view HTML file it works fine. However, calling through the RJS  
file always fails.

As a work around, I tried setting the fields\_for at the highest level  
and passing in the fields\_for object into the RJS file. However, this  
results in the template error undefined method `labelâ€™. This is in  
regards to the first f.label call in the partial.

Basically it seems that the RJS is choking on any/all form helpers.

My prototype version is 1.6.0.1. Rails 2.3.5. Ruby 1.8.6 or 1.8.7

Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 28, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/2 "2010-05-28T17:23:28Z")

</div>

On May 28, 3:25 pm, Steve R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve been spending a great deal of time chasing this problem. Hopefully  
> someone sees something obvious I’m doing wrong. Basically anytime I try  
> to call fields\_for from an RJS file or within a partial in the RJS file,  
> I get the following error. My goal is simply to load a partial into a  
> div. The fields in this partial require fields\_for though. And that’s  
> where the problem lies.
> 
> ActionView::TemplateError (undefined method `fields\_for’ for  
> “#ActionView::Helpers::FormBuilder:0x944ea28”:String) on line #3 of

This is the hint: @form is not a form builder. it’s a string  
containing the text “#ActionView::Helpers::FormBuilder:0x944ea28”

Fred

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 28, 2010, 9:19pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/3 "2010-05-28T21:19:30Z")

</div>

Ahh! That makes sense. Now… 🙂 Fred you’re my new hero!

So the problem seems to be I’m not correctly passing the form\_builder  
from my view file to the controller.

# view file

\<%= observe\_field(“trial\_org\_id”, :url =\> {:action =\> :observe,  
:trial\_id =\> @trial.id, :f=\> f}, :with =\> ‘q’) %\>

# controller file

@form = params[:f]

I was expecting params[:f] to return the object, but as you pointed out  
it is returning a string describing the object.

How can I get observe\_field to pass the object itself?

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 28, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/4 "2010-05-28T21:40:55Z")

</div>

Steve R. wrote:

> Ahh! That makes sense. Now… 🙂 Fred you’re my new hero!
> 
> So the problem seems to be I’m not correctly passing the form\_builder  
> from my view file to the controller.
> 
> # view file
> 
> \<%= observe\_field(“trial\_org\_id”, :url =\> {:action =\> :observe,  
> :trial\_id =\> @trial.id, :f=\> f}, :with =\> ‘q’) %\>
> 
> # controller file
> 
> @form = params[:f]
> 
> I was expecting params[:f] to return the object, but as you pointed out  
> it is returning a string describing the object.
> 
> How can I get observe\_field to pass the object itself?

Why do you want to? Your controller shouldn’t normally be dealing with  
FormBuilder objects. What are you trying to achieve?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 28, 2010, 10:06pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/5 "2010-05-28T22:06:41Z")

</div>

Steve R. wrote:  
[…]

> > > How can I get observe\_field to pass the object itself?
> > 
> > Why do you want to? Your controller shouldn’t normally be dealing with  
> > FormBuilder objects. What are you trying to achieve?
> > 
> > ## Best,
> > 
> > Marnen Laibow-Koser  
> > [http://www.marnen.org](http://www.marnen.org)  
> > removed\_email\_address@domain.invalid
> 
> Hi Marner

Please note correct spelling of my name.

> I have an RJS file that is inserting a partial that has form  
> components in it. I use the observe\_field in the view to detect select  
> box option. That value dictates the partial that gets rendered. Once the  
> observe\_field fires, it runs an action in the controller. The controller  
> then sends the info to the rjs file. As I said, the partials rendered in  
> the RJS file have form elements and thus need the form builder object to  
> be passed from the View-\>controller-\>RJS-\>rendered partial.

No. The controller never needs to touch or know about the form builder.  
That’s defined in the view and only in the view. The controller just  
needs to pass in the dynamic data.

> Is this path possible?

No. You can’t pass a FormBuilder as an HTTP parameter – HTTP  
paramaters are always strings. Fortunately, you don’t need to, as I  
explained above.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 28, 2010, 11:45pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/6 "2010-05-28T23:45:32Z")

</div>

Marnen, my apologies on mispelling your name.

Given the flow of my code “Main html(view)-\>controller-\>RJS-\>rendered  
partial”, the error I recieve seems to be the result of the RJS file  
calling a partial with form elements like “f.text\_area” for example.  
When the form builder object “f” is not passed into the partial that is  
rendered form the RJS file, I get the ActionView::TemplateError.

Are you saying to omit the form builder object “f” in the partial? Such  
that it would read “text\_area” rather than “f.text\_area”?

Would it still work when the HTML is inserted back into the main view  
page?

I’m sorry if I’m not explaining well. I appreciate all the help I’m  
getting.

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 28, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/7 "2010-05-28T21:51:26Z")

</div>

Marnen Laibow-Koser wrote:

> Steve R. wrote:
> 
> > Ahh! That makes sense. Now… 🙂 Fred you’re my new hero!
> > 
> > So the problem seems to be I’m not correctly passing the form\_builder  
> > from my view file to the controller.
> > 
> > # view file
> > 
> > \<%= observe\_field(“trial\_org\_id”, :url =\> {:action =\> :observe,  
> > :trial\_id =\> @trial.id, :f=\> f}, :with =\> ‘q’) %\>
> > 
> > # controller file
> > 
> > @form = params[:f]
> > 
> > I was expecting params[:f] to return the object, but as you pointed out  
> > it is returning a string describing the object.
> > 
> > How can I get observe\_field to pass the object itself?
> 
> Why do you want to? Your controller shouldn’t normally be dealing with  
> FormBuilder objects. What are you trying to achieve?
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

Hi Marner  
I have an RJS file that is inserting a partial that has form  
components in it. I use the observe\_field in the view to detect select  
box option. That value dictates the partial that gets rendered. Once the  
observe\_field fires, it runs an action in the controller. The controller  
then sends the info to the rjs file. As I said, the partials rendered in  
the RJS file have form elements and thus need the form builder object to  
be passed from the View-\>controller-\>RJS-\>rendered partial.

Is this path possible?

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 29, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/8 "2010-05-29T18:57:11Z")

</div>

I actually tried to add form\_for in the partial and it appeared to work.  
The form within a form wasn’t a problem it seemed. However when a  
validation error was detected, the dynamically rendered partial was  
gone. When I tried to reselect the partial in the select box, the  
content I added was wiped out. I’m sure there may be a way to preserve  
the content after form validation, but it seems like too much work.

At this rate I’ll take your advice and seek a different approach using  
pure javascript. I may also try RJS code directly in the main view via  
the :function parameter in the observe\_field method.

Thanks for your help Marnen,

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [May 28, 2010, 11:50pm UTC](https://www.ruby-forum.com/t/form-helpers-dont-seem-to-work-with-rjs-files/189314/9 "2010-05-28T23:50:02Z")

</div>

Steve R. wrote:

> Marnen, my apologies on mispelling your name.
> 
> Given the flow of my code “Main html(view)-\>controller-\>RJS-\>rendered  
> partial”, the error I recieve seems to be the result of the RJS file  
> calling a partial with form elements like “f.text\_area” for example.

Can’t the partial instantiate the FormBuilder? In other words, why  
doesn’t the partial contain the form\_for call?

> When the form builder object “f” is not passed into the partial that is  
> rendered form the RJS file, I get the ActionView::TemplateError.
> 
> Are you saying to omit the form builder object “f” in the partial? Such  
> that it would read “text\_area” rather than “f.text\_area”?

No – there’s no such method.

> Would it still work when the HTML is inserted back into the main view  
> page?
> 
> I’m sorry if I’m not explaining well. I appreciate all the help I’m  
> getting.

You can’t use a FormBuilder to rebuild part of a form AFAIK. I’ll  
admit, however, that I have very little use for RJS – I usually prefer  
to write JavaScript directly.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
