Including another form in a form_remote_tag?

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!

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.

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.

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:

<head> <script> 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         ajax.options.postBody = [$('display_range').serialize(), Object.toQueryString(xhr.options.parameters)].join('&')     }, }); </script> </head>

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('&') } });

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.

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.