create subform within rails form

Hi,

I have a form for a model that have relationship to other models in my
application. For example, a company and employees. When I add new
employee, I should select company for which employee is working. How
about if the employee works in a company that is not in my database? so
I want to create that company within the same form of adding employee
(sub form). Can anyone tell me how to do that in rails?

Ahmed

Hi,

I am doing just that in my application.

First I would like to give you a heads up that HTML does not work well with nested FORMS, at least the browsers do not. So you will have to ensure that the forms aren’t nested, you can still make them appear nested / subforms by using relative positioning however.

Second in my scenario I felt best to have AJAX forms for the creation purpose so that I can create the object without having to leave the main form, you should consider something similar.

Beyond that it depends on your exact scenario that you wish to create.

Thanks & Regards,
Dhruva Sagar.

Hi,

I have a form for a model that have relationship to other models in my

application. For example, a company and employees. When I add new

employee, I should select company for which employee is working. How

about if the employee works in a company that is not in my database? so

I want to create that company within the same form of adding employee

(sub form). Can anyone tell me how to do that in rails?

Ahmed

Ahmed, it seems that you’re needing a multi model form that accepts nested attributes. Thus,

you might want to take a look at the following example:

http://weblog.rubyonrails.org/2009/1/26/nested-model-forms

Good luck,

-Conrad