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
Asked
Active
Viewed 4,754 times
1
-
1You don't need a seperate view. Like KappaNossi said, just create a hash table and you can use one view to constantly refresh and display questions (and you have the corresponding answer easily accesible). Make sure to do this on the client side so its a fluid experience for users. You might be interested in looking into **ajax** for this – Seyon Jan 30 '14 at 14:34
3 Answers
5
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.

KappaNossi
- 2,656
- 1
- 15
- 17
-
-
well, yeah I added html escaping (h) to the template calls if that's what you meant. I just deemed it a minor detail since you were asking for a basic structure. – KappaNossi Jan 30 '14 at 14:57
3
I would suggest to use a nested form with a partial as described in this RailsCast.

Patru
- 4,481
- 2
- 32
- 42
-
I think this method might be better. Because it follows the dry principle right? – anonn023432 Jan 30 '14 at 19:03
1
Look at survey gem https://github.com/NUBIC/surveyor and blog post http://www.runtime-revolution.com/runtime/blog/introducing-survey#.Uut5jJBEyj4 .

Sanjiv
- 813
- 6
- 13