# Including another form in a form\_remote\_tag?

**URL:** <https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407>\
**Category:** Rails\
**Created:** [February 18, 2008, 10:32am UTC](https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407 "2008-02-18T10:32:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@rodk](https://www.ruby-forum.com/u/rodk)\
**Post date:** [February 18, 2008, 10:32am UTC](https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407/1 "2008-02-18T10:32:13Z")

</div>

I have a form which is just populated by a select\_date, and I have  
another form in a form\_remote\_tag,  
and when submitting this latter form, I need to also include the  
select\_date inputs in the former form.  
I see that remote\_function takes a :with option, and that seems to  
work using Form.serialize(), so I tried adding :with and  
Form.serialize to my form\_remote\_tag, but no additional params were  
included.

Is there a rails way of doing this, or is the only way going to be to  
call out to js in a :before option, and make that  
js function dynamically add the form inputs from the external form?

Thanks!

---

<div class="post-metadata">

**Author:** ![rodk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@rodk](https://www.ruby-forum.com/u/rodk)\
**Post date:** [February 18, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407/2 "2008-02-18T20:08:10Z")

</div>

Do you ever need to submit the forms independently (ie., date-only  
form has a submit and the date+others has a submit)? If not, then you  
should restructure your view:

\<% form\_for :date\_only\_model do |f| %\>  
\<%= f.date\_select :my\_date\_field, … %\>  
\<% fields\_for :other\_object do |obj\_fields| %\>  
\<%= obj\_fields.text\_field :first\_one, … %\>  
…  
\<% end %\>  
\<% end %\>

You can use fields\_for to mix fields from different models into the  
same form.

---

<div class="post-metadata">

**Author:** ![rodk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@rodk](https://www.ruby-forum.com/u/rodk)\
**Post date:** [February 18, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407/3 "2008-02-18T21:20:35Z")

</div>

rodk wrote:

> I have a form which is just populated by a select\_date, and I have  
> another form in a form\_remote\_tag,  
> and when submitting this latter form, I need to also include the  
> select\_date inputs in the former form.  
> I see that remote\_function takes a :with option, and that seems to  
> work using Form.serialize(), so I tried adding :with and  
> Form.serialize to my form\_remote\_tag, but no additional params were  
> included.
> 
> Is there a rails way of doing this, or is the only way going to be to  
> call out to js in a :before option, and make that  
> js function dynamically add the form inputs from the external form?
> 
> Thanks!

You could always pass the value along as a parameter to the method and  
then create a value for it in that method so that it could be seen in  
the next form.

---

<div class="post-metadata">

**Author:** ![rodk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@rodk](https://www.ruby-forum.com/u/rodk)\
**Post date:** [February 26, 2008, 10:16am UTC](https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407/4 "2008-02-26T10:16:51Z")

</div>

Bad formatting, let’s try that again:

Ajax.Responders.register({  
onCreate: function(xhr, response) {  
/\* serialize the display\_range form, and add it to the params  
which are generated for every Ajax.Request obj \*/  
xhr.options.postBody = [ $(‘display\_range’).serialize(),  
Object.toQueryString(

xhr.options.parameters)  
].join(’&’)  
}  
});

---

<div class="post-metadata">

**Author:** ![rodk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@rodk](https://www.ruby-forum.com/u/rodk)\
**Post date:** [February 27, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407/5 "2008-02-27T15:14:34Z")

</div>

I’d be afraid that the code you’ve posted is going to attempt to run  
on EVERY page, unless you explicitly unload it elsewhere.

Without a view fragment it’s tough to offer more specific help. Just  
a thought, have you considered reversing things? That is, you could  
put the select in the bundled-up view (the second one) and do  
something more explicit when you want to submit only the date. If you  
did that there’s a good chance that the :with param will work as you  
expected. Another avenue is using the :submit param. It allows you  
do pass a dom id and the contents of that element are serialized and  
submitted as query params on the AJAX call. That might free you up to  
either (a) embed the small form in the larger and submit it using  
submit or (b) put some kind of div container around the date select  
and the form (possibly both forms) and use :submit to send off the  
larger container.

---

<div class="post-metadata">

**Author:** ![rodk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@rodk](https://www.ruby-forum.com/u/rodk)\
**Post date:** [February 26, 2008, 10:12am UTC](https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407/6 "2008-02-26T10:12:07Z")

</div>

After digging around the PrototypeHelper.rb source, I see that  
form\_remote\_tag is just not going to be able to do this specific kind  
of feature, since ‘:with’ is not even an option it will try to use.  
And ‘:before’ doesn’t get access to the Ajax.Request object, so  
there’s no way for it to access the parameters to add anything before  
the form submit is sent.

But looking through Ajax.Request in prototype.js, I noticed a  
ridiculously easy alternative that’s only a few lines of code–it just  
took a while to discover. So just in case anyone else encounters a  
similar problem where they need to append one form’s params to every  
other form on the page before the submit, here’s how I solved it:

---

<div class="post-metadata">

**Author:** ![rodk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@rodk](https://www.ruby-forum.com/u/rodk)\
**Post date:** [March 3, 2008, 5:31am UTC](https://www.ruby-forum.com/t/including-another-form-in-a-form-remote-tag/129407/7 "2008-03-03T05:31:46Z")

</div>

I just had to do something similar to this… I used a temporary  
session variable to carry the post parameter. Once I was done with it  
I cleared it. Seemed to work perfectly.
