某些记录未以嵌套形式保存

时间:2014-06-18 11:02:20

标签: ruby-on-rails ruby forms ruby-on-rails-4 nested-forms

所以我正在做这个测验应用程序。 这是我的测验控制器:

# encoding: utf-8
class QuizzesController < ApplicationController
  before_action :set_quiz, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  # GET /quizzes
  # GET /quizzes.json
  def index
    @quizzes = Quiz.all
  end

  # GET /quizzes/1
  # GET /quizzes/1.json
  def show
  end

  # GET /quizzes/new
  def new
    @quiz = Quiz.new
    10.times do
      question = @quiz.questions.build
      4.times { question.answers.build }
    end
    5.times do 
        @quiz.links.build 
    end
  end

  # GET /quizzes/1/edit
  def edit
  end

  # POST /quizzes
  # POST /quizzes.json
  def create
    @quiz = Quiz.new(quiz_params)

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

  # PATCH/PUT /quizzes/1
  # PATCH/PUT /quizzes/1.json
  def update
    respond_to do |format|
      if @quiz.update(quiz_params)
        format.html { redirect_to @quiz, notice: 'Quiz was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @quiz.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /quizzes/1
  # DELETE /quizzes/1.json
  def destroy
    @quiz.destroy
    respond_to do |format|
      format.html { redirect_to quizzes_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_quiz
      @quiz = Quiz.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def quiz_params
      params.require(:quiz)
      .permit(:content,
      links_attributes: [:id, :name, :www],
      question_attributes: [:id, :content,
      answer_attributes: [:id, :content, :correct]])
    end
end

问题控制员:

# encoding: utf-8
class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]
  before_action :set_quizzes, only: [:new, :show, :edit, :update, :destroy]

  # GET /questions
  # GET /questions.json
  def index
    @questions = Question.all
  end

  # GET /questions/1
  # GET /questions/1.json
  def show
  end

  # GET /questions/new
  def new
    @question = Question.new
  end

  # GET /questions/1/edit
  def edit
  end

  # POST /questions
  # POST /questions.json
  def create
    @question = Question.new(question_params)

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

  # PATCH/PUT /questions/1
  # PATCH/PUT /questions/1.json
  def update
    respond_to do |format|
      if @question.update(question_params)
        format.html { redirect_to @question, notice: 'Question was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /questions/1
  # DELETE /questions/1.json
  def destroy
    @question.destroy
    respond_to do |format|
      format.html { redirect_to questions_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end
    def set_quizzes
    @quizzes = Quiz.find(:all).map do |quiz|
      [quiz.content, quiz.id]
    end
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question)
      .permit(:content, :quiz_id,
      answer_attributes: [:id, :content, :correct])
    end
end

回答:

    # encoding: utf-8
    class AnswersController < ApplicationController
      before_action :set_answer, only: [:show, :edit, :update, :destroy]
      before_action :set_questions, only: [:new, :show, :edit, :update, :destroy]
      # GET /answers
      # GET /answers.json
      def index
        @answers = Answer.all
      end

      # GET /answers/1
      # GET /answers/1.json
      def show
      end

      # GET /answers/new
      def new
        @answer = Answer.new
      end

      # GET /answers/1/edit
      def edit
      end

      # POST /answers
      # POST /answers.json
      def create
        @answer = Answer.new(answer_params)

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

      # PATCH/PUT /answers/1
      # PATCH/PUT /answers/1.json
      def update
        respond_to do |format|
          if @answer.update(answer_params)
            format.html { redirect_to @answer, notice: 'Answer was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: 'edit' }
            format.json { render json: @answer.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /answers/1
      # DELETE /answers/1.json
      def destroy
        @answer.destroy
        respond_to do |format|
          format.html { redirect_to answers_url }
          format.json { head :no_content }
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_answer
          @answer = Answer.find(params[:id])
        end
        def set_questions
          @questions = Question.find(:all).map do |question|
          [question.content, question.id]
        e

nd
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def answer_params
      params.require(:answer).permit(:content, :correct, :question_id)
    end
end

Model:

    class Quiz < ActiveRecord::Base
      has_and_belongs_to_many :users
      has_many :questions
      has_many :links
      accepts_nested_attributes_for :links
      accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
    end

    class Question < ActiveRecord::Base
      belongs_to :quiz
      has_many :answers
      accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
    end

并形成:

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

      <ul>
      <% @quiz.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <p>
    <%= f.label :content, "Nazwa quizu" %>
    <%= f.text_field :content %>
  </p>
    <ol> <%= f.fields_for :links do |builder| %>
    <li><%= render "link_fields", :f => builder %></li>
  <% end %>
  </ol>
  <ol><%= f.fields_for :questions do |builder| %>
    <li><%= render "question_fields", :f => builder %></li>
  <% end %>
</ol>
  <p><%= f.submit "Submit" %></p>

表单适用于链接,但是当涉及到保存问题和答案时,它什么都不做(当我列出数据库中的所有记录时,只有我使用其他表单和方法保存的问题)。下面的代码是为了显示用这个其他表单生成的问题而创建的,但是由于我稍微更改了方法,我是否必须在视图中更改代码?或者也许是控制器故障?

这是我显示的代码:

<div class="row">               
      <div  id="prezentacja" class="col-md-7 col-lg-7 col-sm-12 prezentacja3 center-block3">                
          <p class="header2 roboto">  
            <%= @quiz.content %>
          </p>      
          <%= notice %>         
          <div class="col-md-12 col-lg-12" >
              <% @quiz.questions.each do |question| %>
              <div id="answer-<%= question.id %>" class="question col-md-12 col-lg-12">
                  <h4><%= question.content %></h4>
                  <% question.answers.each do |answer| %> 
                  <div class="col-lg-4 answer">
                    <p><%= answer.content %></p>
                  </div>
                  <% end %>
              </div>
              <% end %>
          </div>
      <%= Answer.all.to_a %>
      </div>                
      <div  id="prezentacja" class=" col-md-3 col-lg-3 col-sm-12 prezentacja3 center-block3">                           
      <div class="row"> 
          <div class="col-md-12 col-lg-12 col-sm-12"> 
              <% @quiz.links.each do |link| %>        
                <%= link_to link.name, "http://#{link.www}" %> 
              <% end %>
          </div>                    
      </div>                                    
      <div class="row">                     
      <div class="col-md-12 col-lg-12 col-sm-12">                       
      <p>Dupa dupa dupa</p>
      </div>                
      </div>                
      </div>
  </div>

1 个答案:

答案 0 :(得分:1)

您的测验控制器中有拼写错误:

def quiz_params
  params.require(:quiz)
  .permit(:content,
    links_attributes: [:id, :name, :www],
    question_attributes: [:id, :content,
    answer_attributes: [:id, :content, :correct]])
end

应该是questions_attributes。同样地,对于&#39; question_params&#39;应该有&#39; answers_attributes&#39;。为什么您的视图中有Dupa dupa dupa? :P