rails best practice for a multistep form?

2019-06-06 21:53发布

问题:

I want to create a mutlistep form in Rails 3. I've watched the railscasts episode on it, but I did not feel like he was using the best practice when creating the form. I felt like it was a sloppy way to accomplish the task. What is the best way to create a multistep form using the best practices in rails?

回答1:

Your question is a little hard to answer without more information, and a true answer would be a detailed tutorial (like a Railscast) rather than a SO answer, but here's some thoughts to get you on your way.

There are two major approaches to multistep forms:

  1. Use Javascript to display the form bit by bit

  2. Create separate views and use create/update or similar to route the user from one to the next.

There are advantages to each method, depending on whether you want to support javascript, and what your requirements are about saving data in between sections.

Advantages of 1

  • Faster for the user to navigate from section to section (javascript hide/show is instantaneous)
  • Data is easily accessible is the user wants to refer to an earlier section
  • Simpler controller actions

Disadvantages of 1

  • Will not work for users who are not running javascript (and no progressive enhancement is really possible here other than displaying the form as a huge chunk).
  • Will require you to provide javascript-based navigation to move from section to section (only a disadvantage if you're new to .js)
  • Will require AJAX if you want to save the user's information between steps.
  • Without AJAX and javascript, the user is at the risk of losing a lot of entry if the user accidentally pressing the back button, etc.