Tuesday, March 15, 2011

In Rails, how can I ensure users enter form data in correct order?

I have a Rails app with a model whose fields are dates which are checkpoints for a certain process.

I want to prevent the user entering a date for the nth checkpoint until they have entered a date for the (n-1)th.

Obviously, I can validate in the model to make sure that no dates are missing (and that dates are in chronological order) but this seems unsatisfactory. Ideally, the nth field should not be visible on the form until the (n-1)th field has been completed (and perhaps a 'next' button has been clicked.

I don't know much about AJAX other than having used it for adding comments on a blog, but it seems it might be useful in this problem. I'm going to take a look at it over the next few days, but I would appreciate it if anybody could give me some pointers.

From stackoverflow
  • Either disable or hide all of the inputs except for the first one. As the user completes each input, use an Ajax call to validate it and then display/enable the next one. Once all the inputs are complete, then display/enable the save/continue button.

    Jason : here's a tutorial on doing show/hide with jquery: http://docs.jquery.com/Tutorials:Basic_Show_and_Hide. There are lots of tutorials and examples of doing Ajax with Rails - every Rails book has a chapter on it. I think your question is too broad to get a meaningful HOWTO type answer.
  • From your description it might be a good use case for a finite state machine. Use the current "state" to determine which date input fields to display on your forms.

    See Github: rubyist/aasm

0 comments:

Post a Comment