Hi, I have been trying to write code for making an online exam
management system. I could not think about a logic to implement the
actual exam.
Here is the problem:
I have a questions table with fields like- id, question, opt1, opt2,
opt3, opt4 and correctans.
I want to read all the records from this table but I want to display
only one record at a time for user to answer the question. User then
clicks next to see the next question and choices and so on till the end
of the records.
I don’t know how to implement this. I did
@questions=Question.find(:all)
Now how can I use this @questions and display only one record at a time
so that user answers the question and then I can do something to store
the user response?
OR if I am wrong, please suggest good logic to implement this.
First of all, online exam must be have category of exam. each exam must
be up to date and never the same each date. out of your case i would
like to try my mind in this case now.
Class Question < ApplicationController
def start_page #I use exam category_id in Questions table #In this case I would like starting Rails Exam with category_id =2 #in Answer table should has column question_id, user_id, time_answer,
answer #dont forget to store your user_id in session[:user_id] #and set up in model for relational model or table.
#here method for user answer question
def answer @used_id = [] << params[:user_answer][:question_id]
params[:user_answer][:user_id]= session[:user_id]
params[:user_answer][:time_answer]= Time.now @user_answer = Answer.new(params[:user_answer])
if @user_answer.save
session[:id_questions] = session[:id_questions] - @used_id
if session[:id_questions].blank?
redirect_to :action=> :complete_page
else
redirect_to :action=> :next_question, :id=>
session[:id_questions].first, :last_page_id => @used_id
end
end
end
For button NEXT PAGE in Question PaGe, put use params next_page and
use this params in metod for showing NEXT PAGE to identify next
question.
The rule is up to you, what you want disable button answer if user use
next or back page button and store it in database such as an Un-answer
page or else.
Thank you brother. You helped me a lot. I did it…
Actually I was implementing someting too simple with single table for
practice. But now I will use multiple tables with relations and a real
working implementation.