Saturday, July 21, 2007

submitting multiple models in rails

I'm redoing the underlying code for Sunshine Organics, it's a great little funky company here in Powell River that delivers organic vegetables, fruit and groceries to your door. Sweet, eh?

I'm redoing the site in Ruby on Rails, and I want to keep the site as close to the original version as possible. One of the actions gives you a big long form with all fields for all the customers, so that you can quickly and easily update things.

I hadn't done this before in RoR, and found this excellent post about how to submit multiple models in a form.

Basically, you want to assign an ID to each form element so that when the form is submitted and the hash is created, a unique form element for each customer field is generated. Then, when you're processing the hash, you just loop over all the indicies in the hash. Neat.