title: Nested forms, running into weird errors
body: <p>I'm making a survey app, I've got 3 models: Survey, Question, and Answer. Survey <code>has_many</code> questions, Question <code>has_many</code> answers.</p>
  
  <p>I've got two main errors:</p>
  
  <ol>
  <li>My code is supposed to generate 4 answer fields for each question but only 1 answer field is generated.</li>
  <li><p>When I press submit on my form, I get </p>
  
  <p><code>unknown attribute 'answers' for Survey.</code>
  Extracted source (around line #33):</p>
  
  <pre><code>  # POST /surveys.json
    def create
      @survey = Survey.new(survey_params)
  
      respond_to do |format|
        if @survey.save
  </code></pre></li>
  </ol>
  
  <p>I think the second problem is related to the answers model in some way but I'm not sure how. Here's my code:</p>
  
  <p>surveys_controller:</p>
  
  <pre><code>class SurveysController &lt; ApplicationController
    before_action :set_survey, only: [:show, :edit, :update, :destroy]
  
    # GET /surveys
    # GET /surveys.json
    def index
      @surveys = Survey.all
    end
  
    # GET /surveys/1
    # GET /surveys/1.json
    def show
    end
  
    # GET /surveys/new
    def new
      @survey = Survey.new
      3.times do 
       question = @survey.questions.build 
       4.times { question.answers.build }
  
        end
    end
  
    # GET /surveys/1/edit
    def edit
  
    end
  
    # POST /surveys
    # POST /surveys.json
    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 :show, status: :created, location: @survey }
        else
          format.html { render :new }
          format.json { render json: @survey.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /surveys/1
    # PATCH/PUT /surveys/1.json
    def update
      respond_to do |format|
        if @survey.update(survey_params)
          format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
          format.json { render :show, status: :ok, location: @survey }
        else
          format.html { render :edit }
          format.json { render json: @survey.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /surveys/1
    # DELETE /surveys/1.json
    def destroy
      @survey.destroy
      respond_to do |format|
        format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_survey
        @survey = Survey.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def survey_params
        params.require(:survey).permit!
      end
  
  
  end
  </code></pre>
  
  <p>surveys/_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@survey) do |f| %&gt;
    &lt;% if @survey.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@survey.errors.count, "error") %&gt; prohibited this survey from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @survey.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
  
    &lt;div class="field"&gt;
      &lt;%= f.label :name %&gt;&lt;br&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
  
      &lt;%= f.fields_for :questions do |builder| %&gt;
    &lt;p&gt;
      &lt;%= builder.label :content, "Question" %&gt;&lt;br /&gt;
      &lt;%= builder.text_area :content, :rows =&gt; 3 %&gt;
    &lt;/p&gt;
  
     &lt;%= f.fields_for :answers do |builder| %&gt;
    &lt;p&gt;
      &lt;%= builder.label :content, "Answer" %&gt;
      &lt;%= builder.text_field :content %&gt;
    &lt;/p&gt;
    &lt;% end %&gt; 
    &lt;% end %&gt; 
  
  
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>surveys/show.html.erb</p>
  
  <pre><code>&lt;p id="notice"&gt;&lt;%= notice %&gt;&lt;/p&gt;
  
  &lt;p&gt;
    &lt;strong&gt;Name:&lt;/strong&gt;
    &lt;%= @survey.name %&gt;
  &lt;/p&gt;
  
  &lt;ol&gt;
    &lt;% @survey.questions.each do |question| %&gt;
    &lt;li&gt;&lt;%= question.content  %&gt;
    &lt;ul&gt;
      &lt;% for answer in question.answers %&gt;
      &lt;li&gt;&lt;%= answer.content %&gt;&lt;/li&gt;
      &lt;% end %&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
      &lt;% end %&gt;
  &lt;/ol&gt;
  
  
  &lt;%= link_to 'Edit', edit_survey_path(@survey) %&gt; |
  &lt;%= link_to 'Back', surveys_path %&gt;
  </code></pre>
  
  <p>survey.rb:</p>
  
  <pre><code>class Survey &lt; ActiveRecord::Base
      has_many :questions, :dependent =&gt; :destroy
      accepts_nested_attributes_for :questions, :reject_if =&gt; -&gt; (a) {a[:content].blank? }, :allow_destroy =&gt; true
  end
  </code></pre>
  
  <p>question.rb:</p>
  
  <pre><code>class Question &lt; ActiveRecord::Base
      belongs_to :survey
      has_many :answers, :dependent =&gt; :destroy
      accepts_nested_attributes_for :answers, :reject_if =&gt; -&gt; (a) {a[:content].blank? }, :allow_destroy =&gt; true
  end
  </code></pre>
  
  <p>answer.rb</p>
  
  <pre><code>class Answer &lt; ActiveRecord::Base
      belongs_to :question
  end
  </code></pre>
  
  <p>Any help would be appreciated, I've been stuck on this for hours now!</p>