Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to structure a multi-page form to create and edit models in a database?

I'm fairly new to Rails.

What is the best way to design/structure a multi-page form in Rails? I would like the form to create a new record in the database immediately when the first page of the form is submitted, and for each subsequent page of the form to update that record in the database.

The reason I want the record created immediately after the first page is submitted is so that there can be the notion of an unfinished record that the user comes back to later to finish.

Once created, I would like to allow the user to edit any part of the model by going directly to that section.

I know enough Rails where if you guide me with the best way to structure this conceptually, I should be able to figure out the code myself.

Thank you.

like image 912
Sanjay Avatar asked Sep 01 '10 15:09

Sanjay


People also ask

What is multi-page form?

Multi-page forms are an extremely popular form type and ideal for those longer forms which require multiple fields. Splitting a form into multiple pages can help provide a user-friendly and well organized layout, whilst preventing the form from feeling overwhelming to users.

What is the structure of a database called?

The database schema is the structure of a database described in a formal language supported by the database management system (DBMS). The term "schema" refers to the organization of data as a blueprint of how the database is constructed (divided into database tables in the case of relational databases).


2 Answers

I have a multi-step signup process that works this way. I create the record the first time and then the other steps are edit/updates on that record. I use multiple controllers for this; it is a much cleaner approach than trying to cram all the logic into one controller action (although you could use multiple actions from the same controller and it would work just as well, but don't forget to create routes for your custom actions). This approach makes validation more difficult for the data added in steps after the first, but you can always add your own errors by calling errors.add on your model, essentially rolling your own validations. You can also write logic in your sessions controller to direct the user back to the same step in the multi-step form if they return later and have not completed it.

like image 95
John Glass Avatar answered Nov 24 '22 18:11

John Glass


Ryan Bates explains this in one of his Railscasts => MultiSteps Forms

like image 30
jpemberthy Avatar answered Nov 24 '22 16:11

jpemberthy