# Code help for implementing online exam please

**URL:** <https://www.ruby-forum.com/t/code-help-for-implementing-online-exam-please/149641>\
**Category:** Rails\
**Created:** [September 27, 2008, 5:08am UTC](https://www.ruby-forum.com/t/code-help-for-implementing-online-exam-please/149641 "2008-09-27T05:08:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rup](https://www.ruby-forum.com/u/rup)\
**Post date:** [September 27, 2008, 5:08am UTC](https://www.ruby-forum.com/t/code-help-for-implementing-online-exam-please/149641/1 "2008-09-27T05:08:45Z")

</div>

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.

Thanks

---

<div class="post-metadata">

**Author:** ![rup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rup](https://www.ruby-forum.com/u/rup)\
**Post date:** [September 27, 2008, 10:20am UTC](https://www.ruby-forum.com/t/code-help-for-implementing-online-exam-please/149641/2 "2008-09-27T10:20:13Z")

</div>

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.

if session[:id\_questions]  
@disable\_button\_answer = true  
else  
@all\_question = Question.find(:all, :conditions=\>[‘category\_id=?’, 2])  
session[:id\_questions] = @questions.map(&:id)  
@first\_question = @all\_question.first  
@user\_answer = Answer.new  
end

end

#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

def next\_question  
@question = Question.find(params[:id])  
@user\_answer = Answer.new  
@back\_page = params[:last\_page\_id]  
@next\_page = session[:id\_questions][1]  
end

* * *

Suggestion :

- 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.

Peace Regards,  
Y. Reinhart A P  
Blog : [teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![rup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rup](https://www.ruby-forum.com/u/rup)\
**Post date:** [September 27, 2008, 10:27am UTC](https://www.ruby-forum.com/t/code-help-for-implementing-online-exam-please/149641/3 "2008-09-27T10:27:13Z")

</div>

Rails T. wrote:

> Rails T. wrote:  
> 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

the correct is :

redirect\_to :action=\> :next\_question, :id=\>  
session[:id\_questions].first[0], :last\_page\_id =\> @used\_id[0]

if you find any error, keep post it, I will review back when I’m online.  
🙂

I hope some my brothers can develop my code and give good suggestion  
from my code or maybe can provide new code. 🙂

Have nice weekend  
Y. Reinhart A P  
Blog : [teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![rup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rup](https://www.ruby-forum.com/u/rup)\
**Post date:** [September 27, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/code-help-for-implementing-online-exam-please/149641/4 "2008-09-27T22:15:51Z")

</div>

Rails T. wrote:

> Rails T. wrote:
> 
> > Rails T. wrote:  
> > 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
> 
> the correct is :
> 
> redirect\_to :action=\> :next\_question, :id=\>  
> session[:id\_questions].first[0], :last\_page\_id =\> @used\_id[0]
> 
> if you find any error, keep post it, I will review back when I’m online.  
> 🙂
> 
> I hope some my brothers can develop my code and give good suggestion  
> from my code or maybe can provide new code. 🙂
> 
> Have nice weekend  
> Y. Reinhart A P  
> Blog : [teapoci.blogspot.com](http://teapoci.blogspot.com)

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.

Thanks  
Great job. Keep doing.

---

<div class="post-metadata">

**Author:** ![rup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rup](https://www.ruby-forum.com/u/rup)\
**Post date:** [September 27, 2008, 10:25am UTC](https://www.ruby-forum.com/t/code-help-for-implementing-online-exam-please/149641/5 "2008-09-27T10:25:06Z")

</div>

Rails T. wrote:

> 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

the correct is :

redirect\_to :action=\> :next\_question, :id=\>  
session[:id\_questions].first, :last\_page\_id =\> @used\_id[0]

if you find any error, keep post it, I will review back when I’m online.  
🙂

I hope some my brothers can develop my code and give good suggestion  
from my code or maybe can provide new code. 🙂

Have nice weekend  
Y. Reinhart A P  
Blog : [teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![rup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rup](https://www.ruby-forum.com/u/rup)\
**Post date:** [October 29, 2008, 6:15am UTC](https://www.ruby-forum.com/t/code-help-for-implementing-online-exam-please/149641/6 "2008-10-29T06:15:51Z")

</div>

hi iwant to send a session variable to the database,can you please  
tell me how can  
i do that

On Sep 27, 9:08 am, Rup R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
