Building a questionnaire in Rails - Display only one field at a time instead of a long list of fields?

573 views Asked by At

I'm using Rails to create a questionnaire application. I have a created a scaffold with the model called Question. The questionnaire has 30+ questions so when I create a new questionnaire I have a long list of questions in a row to answer, generated with the form helper in the form partial. Instead of the long list of 30+ questions I would rather want to display only one question at a time but I'm not sure what solution to use.

I looked into the possibility to use the gem Will Paginate to display the questions one at a time. But it seems that Will Paginate is not designed to be used with a form_for and post method, so it does not seem like a proper solution.

Then I tried to figure out if I could use AJAX to display one question at a time but I could not find any documentation or example were AJAX were used on a form to accomplish this.

The last solution I could think of is to use a separate view or partial per question but it would be a tedious job to create all those files so maybe not a good solution either.

What would be the best way to solve this problem?

1

There are 1 answers

0
Max Williams On BEST ANSWER

I would load the whole form onto the page, with all 30 questions, but hide all except the first question, and hide the submit button too. When someone answers a question, hide it (with javascript) and show the next question (or the submit button if that was the last question). This is simple because

a) you're not doing a multi-page form, which can be a bit hard to handle, especially if you get validation errors.

b) you're not mucking about with ajax. Just simply hiding and showing elements.

c) because it's all on the same page, it's easy to do other javascript enhancements, like a progress bar for example.