Accessing an instance variable in another method

S

Subashini Kumar

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
 
P

Paul Smith

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

=A0def index
# i have retrieved the first five questions from the database for
displaying the #question to user
=A0 =A0@quiz=3DQuiz.find:)all,:limit=3D>5)

=A0end

=A0def report

=A0end

# this method checks the answer and gives score
=A0def 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?

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.
--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
S

Subashini Kumar

Paul said:
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.

--
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)


Hi,

No that doesnt work..
When i try to access @quiz.each {}

i get an error like "there is no each method defined"
 
D

David A. Black

Hi --

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

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
 
I

Ilan Berci

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
 
I

Ilan Berci

Ilan said:
@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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top