在以测验形式保存用户回复时遇到问题

时间:2018-08-07 14:14:16

标签: ruby-on-rails ruby ruby-on-rails-5

我正在尝试创建一个用户可以单击测验的应用程序,它将带他们到可以回答测验问题并将其答案提交到结果页面的页面。

我在保存用户的响应时遇到了麻烦-它只是保存了最后的记录而不是全部记录,或者根本没有保存到数据库。

这是我的代码:

Question.rb:

  belongs_to :quiz
  has_many :answered_questions, dependent: :destroy
  has_many :answers, dependent: :destroy
  accepts_nested_attributes_for :answers, reject_if: :all_blank, allow_destroy: true

Answer.rb:

  belongs_to :question
  has_many :answered_questions, dependent: :destroy

User.rb:

  has_many :answered_questions, dependent: :destroy
  accepts_nested_attributes_for :answered_questions

AnsweredQuestion.rb:

  belongs_to :user
  belongs_to :question
  belongs_to :answer
  belongs_to :quiz

Quiz.rb

  has_many :questions, dependent: :destroy
  has_many :answered_questions, through: :questions, dependent: :destroy
  accepts_nested_attributes_for :answered_questions, reject_if: :all_blank, allow_destroy: true
  accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true

quizzes_controller.rb:

class QuizzesController < ApplicationController

  before_action :user_completed_quiz, only: [:show]


  def show
    @quiz = Quiz.find(params[:id])
    @questions = Question.all
    @answered_questions = current_user.answered_questions.build

    @quiz.answered_questions.build

  end

  def create 
    @quiz = Quiz.new(show_params)
    if @quiz.save
      flash[:success] = "You have created a new quiz!"
      redirect_to @quiz
    else 
      render 'new'
    end
  end


  def results 
    @quiz = Quiz.find(params[:id])
  end

  def post_answered_questions 
    @quiz = Quiz.find(params[:quiz][:id])
    @answered_question = @quiz.answered_questions.build(show_params)
    if @answered_question.save
      flash[:success] = "You have completed the quiz!"
      redirect_to results_quiz_path(params[:quiz][:id])
    else
      render ''
    end
  end

  private

  def user_completed_quiz 
    if(current_user.answered_questions.pluck(:quiz_id).uniq.include?(params[:id].to_i)) 
      redirect_to quizzes_path
    end
  end

  def show_params
    params.require(:quiz).permit(:title, answered_questions_attributes: [:id, :answer_id, :question_id, :user_id, :quiz_id], questions_attributes: [:id, :question_title, :quiz_id, :done, :_destroy, answers_attributes: [:id, :answer_title, :question_id, :quiz_id, :correct_answer, :_destroy]])
  end
end

show.html.erb-测验(表格在其中):

<%= form_for(@answered_questions, url: answered_questions_path, method: "POST") do |f| %>

  <%= @quiz.title %>

  <%= f.hidden_field :id, :value => @quiz.id %>

  <% @quiz.questions.each do |question| %>

    <%= f.fields_for :answered_questions do |answer_ques| %>

      <h4><%= question.question_title %></h4>

      <%= answer_ques.hidden_field :question_id, :value => question.id %>
      <%= answer_ques.hidden_field :quiz_id, :value => @quiz.id %>
      <%= answer_ques.select(:answer_id, options_for_select(question.answers.map{|q| [q.answer_title, q.id]})) %>
    <% end %>
  <% end %>

  <%= submit_tag %>

<% end %> 

0 个答案:

没有答案