Multi Model Forms With Rails
Ruby on Rails still doesn't have a good story to tell with regard to multi-model forms. Multi-model forms are HTML forms that have fields from more than one model, which the user edits and submits as one. Rails should take this single collection of fields, split it up into multiple models, and create, edit, or delete as necessary. Unfortunately, this still requires quite a bit of work.
I've collected a set of links that may help those that are new handle multiple models in HTML forms with Rails. None of these are the "official" answer to this complex question. However, it appears that Rails core will one day have an out-of-the-box solution to this. Read on.
Ryan Bates, of Railscasts fame, has probably the best known solution to this problem. He suggests Complex Forms Part 1, Complex Forms Part 2, and Complex Forms Part 3. However, as even he mentions, it doesn't work with Rails 2. Refer to these screencasts, and their sometimes useful comments, as background study.
Ryan offers an alternative for Rails 2 and above. He extended his original screencasts, fixed them for Rails 2, and published them in the Advanced Rails Recipes book, published by the Pragmatic Programmers. You'll want to check out Recipe 13, titled "Handle Multiple Models in One Form" (but you really want the whole book, lots of little gems in there).
An alternative to Ryan's methods can be found at attribute_fu, and Rails plugin by James Golick. attribute_fu is similar to Ryan Bates' code, however it's packaged as a plugin for easy install and use, and uses more conventions to cut down on code. Complete with form helpers and extensions to has_many, you should try this plugin. I don't know if it works with deeply nested models, though.
In July of 2008, there was a glimmer of hope that the Rails core was going to get a blessed solution to this problem. Ryan Daigle, or Ryan's Scraps, reported that Nested Model Mass Assignment was added into Rails! However, it was pulled by the core team because they felt it wasn't ready for prime time. There was a lot of discussion about if and when Rails would have nested model mass assignment. Hopefully it will arrive after 2.2. This is a good discussion and links to other plugins or proposals to handle this tricky problem.
Ryan Bates followed the debate and collected and summarized the various different methods and proposals to handle nested model and nested forms with Rails.
I, for one, will be trying attribute_fu. I've used the Advanced Rails Recipes solution, which certainly works. However, it always felt like too much work for me.
Update: it seems that I wrote about handling collections of models with Rails back in 2007.
I've collected a set of links that may help those that are new handle multiple models in HTML forms with Rails. None of these are the "official" answer to this complex question. However, it appears that Rails core will one day have an out-of-the-box solution to this. Read on.
Ryan Bates, of Railscasts fame, has probably the best known solution to this problem. He suggests Complex Forms Part 1, Complex Forms Part 2, and Complex Forms Part 3. However, as even he mentions, it doesn't work with Rails 2. Refer to these screencasts, and their sometimes useful comments, as background study.
Ryan offers an alternative for Rails 2 and above. He extended his original screencasts, fixed them for Rails 2, and published them in the Advanced Rails Recipes book, published by the Pragmatic Programmers. You'll want to check out Recipe 13, titled "Handle Multiple Models in One Form" (but you really want the whole book, lots of little gems in there).
An alternative to Ryan's methods can be found at attribute_fu, and Rails plugin by James Golick. attribute_fu is similar to Ryan Bates' code, however it's packaged as a plugin for easy install and use, and uses more conventions to cut down on code. Complete with form helpers and extensions to has_many, you should try this plugin. I don't know if it works with deeply nested models, though.
In July of 2008, there was a glimmer of hope that the Rails core was going to get a blessed solution to this problem. Ryan Daigle, or Ryan's Scraps, reported that Nested Model Mass Assignment was added into Rails! However, it was pulled by the core team because they felt it wasn't ready for prime time. There was a lot of discussion about if and when Rails would have nested model mass assignment. Hopefully it will arrive after 2.2. This is a good discussion and links to other plugins or proposals to handle this tricky problem.
Ryan Bates followed the debate and collected and summarized the various different methods and proposals to handle nested model and nested forms with Rails.
I, for one, will be trying attribute_fu. I've used the Advanced Rails Recipes solution, which certainly works. However, it always felt like too much work for me.
Update: it seems that I wrote about handling collections of models with Rails back in 2007.