嵌套表单,遇到奇怪的错误

时间:2016-06-18 08:01:12

标签: ruby-on-rails

我正在制作一个调查应用程序,我有3个模型:调查,问题和答案。调查has_many个问题,问题has_many答案。

我有两个主要错误:

  1. 我的代码应该为每个问题生成4个答案字段,但只生成1个答案字段。
  2. 当我在表单上按提交时,我

    unknown attribute 'answers' for Survey. 提取的来源(第33行):

      # POST /surveys.json
      def create
        @survey = Survey.new(survey_params)
    
        respond_to do |format|
          if @survey.save
    
  3. 我认为第二个问题在某种程度上与答案模型有关,但我不确定如何。这是我的代码:

    surveys_controller:

    class SurveysController < 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
    

    调查/ _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 |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>
    
    
      <div class="field">
        <%= f.label :name %><br>
        <%= f.text_field :name %>
      </div>
    
        <%= f.fields_for :questions do |builder| %>
      <p>
        <%= builder.label :content, "Question" %><br />
        <%= builder.text_area :content, :rows => 3 %>
      </p>
    
       <%= f.fields_for :answers do |builder| %>
      <p>
        <%= builder.label :content, "Answer" %>
        <%= builder.text_field :content %>
      </p>
      <% end %> 
      <% end %> 
    
    
    
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
    

    调查/ show.html.erb

    <p id="notice"><%= notice %></p>
    
    <p>
      <strong>Name:</strong>
      <%= @survey.name %>
    </p>
    
    <ol>
      <% @survey.questions.each do |question| %>
      <li><%= question.content  %>
      <ul>
        <% for answer in question.answers %>
        <li><%= answer.content %></li>
        <% end %>
      </ul>
      </li>
        <% end %>
    </ol>
    
    
    <%= link_to 'Edit', edit_survey_path(@survey) %> |
    <%= link_to 'Back', surveys_path %>
    

    survey.rb:

    class Survey < ActiveRecord::Base
        has_many :questions, :dependent => :destroy
        accepts_nested_attributes_for :questions, :reject_if => -> (a) {a[:content].blank? }, :allow_destroy => true
    end
    

    question.rb:

    class Question < ActiveRecord::Base
        belongs_to :survey
        has_many :answers, :dependent => :destroy
        accepts_nested_attributes_for :answers, :reject_if => -> (a) {a[:content].blank? }, :allow_destroy => true
    end
    

    answer.rb

    class Answer < ActiveRecord::Base
        belongs_to :question
    end
    

    任何帮助都会受到赞赏,我已经被困在这几个小时了!

1 个答案:

答案 0 :(得分:1)

我不知道错误是否与fields_for相关,当你这样做时会发生什么:

 <%= f.fields_for :questions do |question_attribute| %>
  <p>
    <%= question_attribute.label :content, "Question" %><br />
    <%= question_attribute.text_area :content, :rows => 3 %>
  </p>

   <%= question_attribute.fields_for :answers do |answer_attribute| %>
    <p>
      <%= answer_attribute.label :content, "Answer" %>
      <%= answer_attribute.text_field :content %>
    </p>
   <% end %> 
 <% end %> 

让我知道结果是什么。