I am working on a quiz game using ruby on rails. I have created the basic authentication and other pages. Now I am starting work on creating the main game. I want to get an opinion about what method should I use i.e. create a seperate view for each question or is there a gem to do the same? Or some other method
I would suggest to use a nested form with a partial as described in this RailsCast.
Well the questions follow a pattern, don't they?
They have a question text and a certain number of answers. One of these answers is defined as 'correct'. Something like this could be appropriate:
(this is just an attribute representation of question
and answer
objects. use actual models and save the values to the database!)
# Question:
{ :question_id => 1,
:text => 'What is StackOverflow?',
:answers => # Answers:
[{:answer_id => 1, :text => 'A search engine'},
{:answer_id => 2, :text => 'An info page for flood victims'},
{:answer_id => 3, :text => 'A website for asking coding related questions'} ],
:correct_answer_id => 3 }
Now use a basic template to display the general question
values and list all nested answer
objects.
<p><%=h @question.text %></p>
<ol>
<% @question.answers.each do |answer| %>
<li><%=h answer.text %></li>
<% end %>
</ol>
Extend this to an actual form to allow submitting of answers and you're set.
Look at survey gem https://github.com/NUBIC/surveyor and blog post http://www.runtime-revolution.com/runtime/blog/introducing-survey#.Uut5jJBEyj4 .
来源:https://stackoverflow.com/questions/21458355/a-quiz-game-on-ruby-on-rails