Hi,
I am new to Rails.I tried developing a basic quiz application.Please
help me out in my doubt.Thanks in advance
<% CODE %>
class QuizController < ApplicationController
def index
i have retrieved the first five questions from the database for
displaying the #question to user
@quiz=Quiz.find(:all,:limit=>5)
end
def report
end
this method checks the answer and gives score
def checkanswer
here i want to access the @quiz object which is a set of questions i
have #retreived from the database.Hot to access it?
end
end
On Tue, Sep 22, 2009 at 1:28 PM, Subashini K.
[email protected] wrote:
displaying the #question to user
here i want to access the @quiz object which is a set of questions i
have #retreived from the database.Hot to access it?
Just use @quiz. That’s the point of instance variables. However, you
have to make sure that checkanswer is being called at some point after
index has been called, and I’m no Rails aficionado.
end
–
Paul S.
http://www.nomadicfun.co.uk
[email protected]
Paul S. wrote:
On Tue, Sep 22, 2009 at 1:28 PM, Subashini K.
[email protected] wrote:
displaying the #question to user
here i want to access the @quiz object which is a set of questions i
have #retreived from the database.Hot to access it?
Just use @quiz. That’s the point of instance variables. However, you
have to make sure that checkanswer is being called at some point after
index has been called, and I’m no Rails aficionado.
�end
–
Paul S.
http://www.nomadicfun.co.uk
[email protected]
Hi,
No that doesnt work…
When i try to access @quiz.each {}
i get an error like “there is no each method defined”
Hi –
On Tue, 22 Sep 2009, Subashini K. wrote:
displaying the #question to user
here i want to access the @quiz object which is a set of questions i
have #retreived from the database.Hot to access it?
end
end
There are two things you have to keep in mind here. The first is that
every instance of a class has its own supply of instance variables:
class Person
def initialize(name)
@name = name
end
def print_name
puts @name
end
end
a = Person.new(“David”)
a.print_name # David
b = Person.new(“Joe”)
b.print_name # Joe
So every time I create a new Person object, that object has a fresh
“slate” of instance variables.
The second thing you need to know is that in your Rails application,
every request creates a new controller object. So the @quiz variable
you create for the index request is not going to exist on the next
request (i.e., when the person looking at the index clicks on a link
and your application goes through another request/response cycle).
Instance variables are “sticky”, but only for the lifetime of the
object.
David
perhaps something like this…
NOT TESTED… at work… just pseudo code to get you started and give you
ideas… not responsible for absolutely anything including but not
limited to your computer blowing up from running the code below.
class QuizController < ApplicationController
def index
@quizes=Quiz.find(:all,:limit=>5) # TODO shake this up with a random
to get unique quizes each time
end
def report
@report = “If you aren’t using ruby, you failed!”
end
def checkanswer
@quiz = Quiz.find(params[:quiz_id]) # get the quiz from the response
form
answers = @quiz.answers
responses = params[:responses] # get the responses from the
response form
@correct_answer_count = answers.zip(responses).map(0) {|sum, a, r| a
== r ? sum + 1 : sum}
@percentage = (@correct_answer_count /
@quiz_completed.questions.size) * 100
end
end
Congrats on getting busy with rails, you are doing great and keep going!
ilan
Ilan B. wrote:
@percentage = (@correct_answer_count /
@quiz_completed.questions.size) * 100
end
ilan
whoops… typo from variable rename…
@percentage = (@correct_answer_count / @quiz.questions.size) * 100
ilan