2014-01-13 133 views
1

嵌套模型上的Noob问题。Rails 4 - 嵌套模型(2级)不保存

我用Rails 4,并试图建立嵌套模型如下:

调查,有许多问题 每个问题有很多答案

我下面Rails Casts episode #196创建的一个调查,问题和答案同样的形式。 Surevey和Realted问题得到保存,但答案没有保存到数据库(答案字段显示正确)。

我真的很感激您对此的意见。

谢谢, 迈克

surveys_controller.rb

def index 
    @surveys = Survey.all 
end 

def new 
    @survey = Survey.new 
    3.times do 
    question = @survey.questions.build 
    1.times { question.answers.build } 
    end 
end 

def create 
    @survey = Survey.new(survey_params) 

    respond_to do |format| 
    if @survey.save 
     format.html { redirect_to @survey, notice: 'Survey was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @survey } 
    else 
     format.html { render action: 'new' } 
     format.json { render json: @survey.errors, status: :unprocessable_entity } 
    end 
    end 
end 

def survey_params 
    params.require(:survey).permit(:name,questions_attributes:[:content,answer_attributes:[:content]]) 
end 

new.html.erb

<h1>New survey</h1> 
    <%= render 'form' %> 
<%= link_to 'Back', surveys_path %> 

_form.html.erb: 

<%= form_for(@survey) do |f| %> 
    <% if @survey.errors.any? %> 
     <div id="error_explanation"> 
      <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2> 
      <ul> 
      <% @survey.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
      <% end %> 
      </ul> 
      </div> 
    <%end%> 

    <div class="field"> 
    <%= f.label :name %><br> 
    <%= f.text_field :name %> 
    </div> 

    <!--Display Questions --> 
    <%= f.fields_for :questions do |builder| %> 
    <%= render 'question_fields', :f => builder%> 
    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 

<% end %> 

_questions_fields.html.erb:

<p> 
<%= f.label :content, "Question" %><br /> 
<%= f.text_area :content, :rows => 3 %> 
</p> 

<!--Display Answers --> 
<%=f.fields_for :answers do |builder| %> 
    <p> 
    <%= render 'answer_fields', :f => builder%> 
    </p> 
<%end%> 

_answers_fields.html.erb:

<p> 
<%= f.label :content, "Answer" %> 
<%= f.text_field :content%> 
</p> 

调查型号:

class Survey < ActiveRecord::Base 
    has_many :questions, :dependent => :destroy 
    accepts_nested_attributes_for :questions 
end 

问题型号:

class Question < ActiveRecord::Base 
    belongs_to :survey 
    has_many :answers, :dependent => :destroy 
    accepts_nested_attributes_for :answers 
    end 

答型号:

class Answer < ActiveRecord::Base 
    belongs_to :question 
    end 
+0

我在阅读时注意到的一件事是您使用'1.times {blah}',但您不应该这样做,因为默认情况下,普通代码行运行1次 –

回答

2

编辑:尝试在survey_params方法变更answer_attributesanswers_attributes

Prying会告诉你答案属性没有通过。


经过快速浏览,我没有看到任何特别错误,所以你只需要调试。您应该添加gem 'pry-rails'到您的Gemfile,然后把

require 'pry'; binding.pry 

你想上哪儿调试线。在调查控制器中创建操作的保存之前,我会把它放好。然后提交表单并转到您的服务器。它会暂停,并且你将有一个控制台。类型

@survey.save 
@survey.errors 

看看会发生什么。同时输入paramssurvey_params以确保您的表单数据全部正确。

如果你在不同的地方盯着你,你会发现什么不起作用。我怀疑你的答案没有被正确地拉入survey_params。当事情没有得到保存时,强属性通常是罪魁祸首。

+0

Bingo!谢谢vramon! –