Topic: Method from within other method

I am trying to call question_and_annos from within questions_and_answers and  pass "complete_question" as an argument. I get the error undefined local variable or method `complete_question' for #<Question:0x103228d10>

Excuse the very basic level of code as I am still a beginner (any suggestions are appreciated though).

Here are the 2 methods:

def question_and_annos(x)
    answer_list = [
      [self.incorrect_ans_1, self.incorrect_anno_1],
      [self.incorrect_ans_2, self.incorrect_anno_2],
      [self.incorrect_ans_3, self.incorrect_anno_3],
      [self.incorrect_ans_4, self.incorrect_anno_4]
      ].shuffle
      # Randomly insert the correct answer and response into the pre-shuffled array.
      random_insert = rand(4)
      answer_list.insert(random_insert,["#{correct_ans_1} *",self.correct_anno])
      #  Create Hash of virtual attributes.
      #  Convert from array to string to display in view "questions.show.html.erb".

      shuffled = {
        :complete_question => "#{question} \r\r\r A.  #{answer_list[0][0]}\r B.  #{answer_list[1][0]}\r C.  #{answer_list[2][0]}\r D.  #{answer_list[3][0]}\r E.  #{answer_list[4][0]}\r",
        :anno_a => answer_list[0][1],
        :anno_b => answer_list[1][1],
        :anno_c => answer_list[2][1],
        :anno_d => answer_list[3][1],
        :anno_e => answer_list[4][1]
        }
       
      if x == "complete_question": result = shuffled[complete_question]
        elsif x == "anno_1": result = shuffled[anno_1]
        elsif x == "anno_2": result = shuffled[anno_2]
        elsif x == "anno_3": result = shuffled[anno_3]
        elsif x == "anno_4": result = shuffled[anno_4]
        elsif x == "anno_5": result = shuffled[anno_5]
      end
         
  end
 
   
def question_with_answers
  x = question_and_annos(complete_question)
end

Thanks,

DC

Re: Method from within other method

def question_with_answers
  x = question_and_annos("complete_question")
end

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Method from within other method

AND

      if x == "complete_question": result = shuffled[:complete_question]

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Method from within other method

Got it, thanks for the help!!
DC

Re: Method from within other method

I appreciate all the help and have one more question that would help me solve this problem.

Should I be able to call methods in a model from inside methods in the controller...  I am trying to call setup_question_and_annos, which is a method in the model, from _anno_1.  _anno_1 is a virtual attribute for a partial.

In the model:
def setup_question_and_annos(x) ....

In the controller:
def _anno_1
    @question = Question.find(params[:id])
    render :layout => false
    x = setup_question_and_annos("anno_1")
end

I get a noMethod Error for setup_question_and_annos in the controller

Last edited by dcastellano1 (2010-08-01 10:24:31)

Re: Method from within other method

try, x=@question.setup_question_and_annos(

Re: Method from within other method

That did it...  Thanks!

DC