2014-01-12 16 views
0

我認爲我的觀點結構不正確,但這是我能夠讓所有表單字段出現的唯一方法。對於嵌套窗體,編輯/更新控制器和窗體視圖是什麼樣的?

我試圖做一個有兩個嵌套模型。我知道最好的做法是隻有一個嵌套模型,所以我似乎無法找到解決這個問題的方法。

我的#edit頁面僅傳遞雙嵌套字段的單個參數。因此,#update控制器未正確更新模型。

查看

## edit.html.erb 
<%= form_for :question, url: scenario_question_path(), method: :patch do |f| %> 
{{ ...error & non-nested inputs }} 
    <ol> 
    <% @question.answers.each do |fa| %> 
     <%= f.fields_for :answers, fa do |ff| %> 
     <li> 
      <%= ff.text_field :answeroption %> 
     </li> 
     <% end %> 
    <% end %> 
    </ol> 
{{ submit }} 

控制器

## questions_controller.rb 
def update 
    @scenario = Scenario.find(params[:scenario_id]) 
    @question = @scenario.questions.find(params[:id]) 

    if @question.update(params[:question].permit(:questionprompt, :text, answers: [:answeroption])) 
     redirect_to scenario_question_path 
    else 
     render 'edit' 
    end 
    end 

    def edit 
    @scenario = Scenario.find(params[:scenario_id]) 
    @question = @scenario.questions.find(params[:id]) 
    @questions = @scenario.questions.all 
    @answers = @question.answers.all 
    end 

編輯提交時傳遞的PARAMS。問題是「答案」實際上有不止一個變化的字段,但只有第一個出現。

{"utf 8"=>"✓", 
"_method"=>"patch", 
"authenticity_token"=>"nvydgO4oxCo58y4gmRAJ5P8Kc+kmbqWGoQ0IjIuzYiQ=", 
"question"=>{"media"=>"test.jpg", 
"questionprompt"=>"123123123", 
"answers"=>{"answeroption"=>"2344634"}}, 
"commit"=>"Save Question", 
"scenario_id"=>"1", 
"id"=>"1"} 

回答

0

我輸出json,並意識到模型的構造方式與我意識到的不同。此外,該視圖四次調用相同的字段,而不是調用四個不同的字段。