A quiz game on ruby on rails [closed]

半腔热情 提交于 2019-12-04 19:03:28

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!