Search code examples
ruby-on-rails-3multi-step

Multiple model form


Let say that I have an interface for inputting vehicle marketing information


   +--------------------+
   | Vehicle            |
   |--------------------|
   |                    |      +--------------------------------------------------+
   | Marketing info     +----->| Marketing info                                   |
   +--------------------+      |--------------------------------------------------|
   |                    |      |                                                  |
   | Engine info        |      |                                                  |
   +--------------------+      |                  +--------------------------+    |
   |                    |      |   Name           |                          |    |
   | Wheels             |      |                  |                          |    |
   +--------------------+      |                  +--------------------------+    |
   |                    |      |                                                  |
   | Doors              |      |                  +--------------------------+    |
   +--------------------+      |   Codename       |                          |    |
   |                    |      |                  |                          |    |
   | Seats              |      |                  +--------------------------+    |
   +--------------------+      |                                                  |
                               |                  +--------------------------+    |
                               |   Disinformation |                          |    |
                               |                  |                          |    |
                               |                  |                          |    |
                               |                  |                          |    |
                               |                  +--------------------------+    |
                               +--------------------------------------------------+

I am in a the first page for that vehicle, I want a vehicle to have it's information save in different tables, for example it can have one marketing_info but many wheels or doors

The problem that I am seeing is that the menu on the left has to link to the related model's forms from the new action so the link helpers will encounter nil id's

If I used just one big form and hid the sections I didn't want the users to see, would that be the best option?

I would also like that form to save as it goes along but then move along the steps in the form, how can I do that? Would I have to redirect to the edit action using an anchor to the next step?

Should I do the following:

  • One big form with hidden steps, with multiple submit buttons on each step
  • a step is shown if it is in the anchor i.e #step1 or in the examples case #marketinginfo
  • I want to save on any of the steps.
  • Once I have saved I need to be in the edit view

Solution

  • Basically you want multi step form http://railscasts.com/episodes/217-multistep-forms and nested form bcz info goes into different tables http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

    you can have different action for each step of form fill-up