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 -- Posted via http://www.ruby-forum.com/.
David A. Black
2009-Sep-22 12:53 UTC
Re: Accessing the instance variable in another method
Hi -- See my answer on ruby-talk. David -- David A. Black, Director Ruby Power and Light, LLC (http://www.rubypal.com) Ruby/Rails training, consulting, mentoring, code review Book: The Well-Grounded Rubyist (http://www.manning.com/black2)
Marnen Laibow-Koser
2009-Sep-22 15:20 UTC
Re: Accessing the instance variable in another method
Subashini Kumar wrote:> 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)You should really call this variable @quizzes, so it will be clear that it contains an Array of Quiz objects, not just one.> > 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?You''ll need to retrieve the records again, or pass them in from params or the session. Instance variables do not persist between controller method calls (because the controller is freshly instantiated each time a request is made).> end > > endBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Saurabh Peshkar
2009-Sep-22 15:21 UTC
Re: Accessing the instance variable in another method
Subashini Kumar wrote:> 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> > endhi Subashini, I think you can use @quiz variable by making it a class variable as @@quiz or a global variable $quiz. might help, Saurabh -- Posted via http://www.ruby-forum.com/.
Abhinav Saxena
2009-Sep-22 15:43 UTC
Re: Accessing the instance variable in another method
On Tue, Sep 22, 2009 at 8:51 PM, Saurabh Peshkar < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > hi Subashini, > > I think you can use @quiz variable by making it a class variable as > @@quiz or a global variable $quiz. > > Certainly, a wrong way to do it. Don''t do it (at least in rails controller)As suggested earlier, you should pass ids in params or store in session to retrieve in your check_answer method.> might help, > > Saurabh > -- > Posted via http://www.ruby-forum.com/. > > > >-- अभिनव http://twitter.com/abhinav --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marnen Laibow-Koser
2009-Sep-22 16:07 UTC
Re: Accessing the instance variable in another method
Saurabh Peshkar wrote: [...]> hi Subashini, > > I think you can use @quiz variable by making it a class variable as > @@quiz or a global variable $quiz.DON''T DO THAT! That will cause other problems.> > might help, > > SaurabhBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.