What is the best way to structure a multi-page form for creating and editing models in a database?

I am new to Rails.

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

The reason I want the post created right after the first page to be presented is that there might be the concept of an incomplete post that the user comes back to later to finish.

After creation, I would like to allow the user to edit any part of the model by going directly to this section.

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

Thanks.

+4
source share
3 answers

I have a multi-step registration process that works this way. I create an entry for the first time, and then the rest of the steps edit / update this entry. For this, I use several controllers; this is a much cleaner approach than trying to overtake all the logic into a single controller action (although you could use several actions from the same controller and it would work just as well, but remember to create routes for your custom actions). This approach simplifies the validation of the data added in the steps after the first, but you can always add your own errors by calling errors.add on your model, essentially reducing your own checks. You can also write logic in your session controller to redirect the user to the same step in a multi-step form if they return later and do not complete it.

+6
source

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

+5
source

I have inherited a β€œmulti-page” form along these lines, but it was created for Rails 2.2, and I'm just adapting the approach for Rails 3.

In fact, we used a tabbed layout with the whole form in one view - although this approach was heavily weighted against a single controller in Rails 2.2. I think it can be beaten better.

The layout meant that each section of the form could be accessed using tabs, but each section with tabs also had a link_to action for the next section, at the bottom of this section (for example, section A β†’ B) that saved the entire form every time you clicked to a new section - I strongly edited the view only to give a view, but if it is a new form, it will show only each section after clicking the submit button for each section.

<ul id="tabs"> <li><a href="#SectionA">Section A</a></li> <li><a href="#SectionB">Section B</a></li> <li><a href="#SectionC">Section C</a></li> <li><a href="#SectionD">Section D</a></li> <li><a href="#SectionE">Section E</a></li> <li><a href="#SectionF">Section F</a></li> <li><a href="#SectionG">Section G</a></li> <li><a href="#SectionH">Section H</a></li> <li><a href="#SectionI">Section I</a></li> <li><a href="#SectionJ">Section J</a></li> </ul> <%=hidden_field_tag 'active_fabtabulous_tab'%> <% form_for(@detail) do |f| %> <%= f.error_messages %> <div class="panel" id="SectionA"> <b><u>Section A: Questionnaire Details</u></b> <br></br> <table> <tr> <td><div id="field_name">Questionnaire received on (dd/mm/yyyy):</div></td> <td><%= date_select("questionnaire", :received_on, :order=>[:day,:month,:year],:use_month_numbers=>true,:start_year=>Date.today.year,:end_year=>2008,:include_blank => true) %></td> </tr> <tr> <td><div id="field_name">Interviewer name:</div></td> <td><%=text_field("questionnaire",:intervieweename)%></td> </tr> </table><!-- end questionnaire div --> <%= f.submit "SectionB" , :class => "questButton" %> </div> <!--- Page 2 ---> <div class="panel" id="SectionB"> <b><u>Section B: Case Classification</u></b> <br></br> <% fields_for :patient, @patient do |p| %> <table> <tr> <td class="sectionA_is_this_case"><div id="field_name">Epidemiology definition:</div></td> <td><%= @patient.epidef %> </td> </tr> </table> <% end %> <table> <tr> <% fields_for :patient, @patient do |p| %> <td><div id="field_name">Asymptomatic:</div></td> <td><% if @patient.asymptomatic %>Yes<% else %>No<% end %></td> <% end %> <tr> <tr> <td><div id="field_name">Investigation is:</div></td> <td><%=select("detail", "invstatus", INVESTIGATION_IS)%></td> </tr> <tr> <td><div id="field_name">Outbreak keyword or number:</div></td> <td><%= f.text_field :outbreakid ,:cols => 40, :rows => 1 %></td> </tr> </table> </div> <%= f.submit "SectionC" , :class => "questButton" %> </div> 
0
source

All Articles