2012-05-04 7 views
0

私はrails/RhoMobileの新機能ですので、親オブジェクトに子オブジェクトを追加する方法を考えることができません。私のフォーム上のインスタンス変数@questionsのメンバー。ここに私のコントローラメソッド@questionsでHTMLページのrails/rhomobileオブジェクトのリストに従属オブジェクトを追加する方法

def diary_day_one_morning 
    qgroup = "activity_day_one_morning" 
    @questions = Question.find(:all, :conditions => { { :name => "qgroup", :op => '=' } => qgroup }) 
    # make sure all questions have an answer 
    @questions.each do |q| 
    a = Answer.find(:all, :conditions => { { :name => "question_id", :op => '=' } => q.object }).first 
    if not a 
     a = Answer.new 
     a.question_id = q.object 
     a.save 
    end 
    #@questions[q].answer = a # here is my issue!!! 
    end 
    render :action => :diary_day_one_morning, :back => url_for(:action => :index) 
end 

[Q]私は私の質問の答えに接合されたようにする方法へと失われたビットだ.answer部分があります。その後、フォームでどのように答えを参照するのですか?ここに私がすでに持っているものがあります:

<% @questions.each_with_index do |question, qcount| %> 
     <div class="ui-block-a"> 
     <div class="ui-bar ui-bar-e"> 
      <%= question.qpromptshort %> 
     </div> 
     </div> 
     <% a = Answer.find(:all, :conditions => { { :name => "question_id", :op => '=' } => question.object }).first %> 

     <% if a %> 
     <input type="hidden" id="answer<%=qcount%>[object]" name="answer<%=qcount%>[object]" value="<%=a.object%>" /> 
     <div class="ui-block-b"> 
      <input type="text" id="answer<%=qcount%>[value1]" name="answer<%=qcount%>[value1]" value="<%=a.value1%>" /> 
     </div> 
    ....... 

私は質問のメンバーとして質問の代わりに参照することを好むでしょう。

答えて

0

@questions配列の各オブジェクトのインデックスは、each_with_indexメソッドを使用して取得できます。

@questions.each_with_index do |q,qindex| 
    a = Answer.find(:all, :conditions => { { :name => "question_id", :op => '=' } => q.object }).first 
    if not a 
     a = Answer.new 
     a.question_id = q.object 
     a.save 
    end 
    # use qindex to add it back into the @questions array 
    @questions[qindex].answer = a 
    end 

このページでは、answer.answerとしてご利用いただけます。

関連する問題