未知属性“ answered_questions_attributes”

时间:2018-08-07 12:37:56

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

我正在尝试创建一个表单,用户可以在其中回答问题并提交到结果页面,但是,当我试图通过Quiz控制器传递AnsweredQuestions属性时,它告诉我这是一个“未知属性”。 / p>

我收到的错误:answeredQuestion的未知属性'answered_questions_attributes'。

Quiz.rb:

 class Quiz < ApplicationRecord
  validates :title, presence: true, length: { maximum: 50 }
  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
end

AnsweredQuestion.rb:

class AnsweredQuestion < ApplicationRecord
  belongs_to :user
  belongs_to :question
  belongs_to :answer
  belongs_to :quiz
end

测验控制器:

  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 post_answered_questions 
    @answered_question = current_user.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(@quiz, url: post_answered_questions_quizzes_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 %> 

已更新:

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 %> 

AnsweredQuestion控制器:

class AnsweredQuestionsController < ApplicationController


  def show
    @answered_question = AnsweredQuestion.new
  end

  def create
    @answered_question = current_user.answered_questions.build(answered_params)
    binding.pry
    if @answered_question.save
      flash[:success] = "You have completed the quiz!"
      redirect_to results_quiz_path(params[:quiz][:id])
    else
      render ''
    end
  end

  def edit
    @answered_questions = AnsweredQuestion.find(params[:id])
  end


  def destroy
    AnsweredQuestion.find(params[:id]).destroy
    flash[:success] = "Answered quiz deleted"
    redirect_to answered_questions_url
  end

  private


  def answered_params
    params.require(:answered_questions).permit(:question_id, :answer_ids, :user_id, :quiz_id, :id, :_destroy)
  end

end

1 个答案:

答案 0 :(得分:1)

似乎您正在接受Quiz的嵌套属性,但是在AnsweredQuestion中而不是在Quiz中使用嵌套属性。您需要在其他模型中添加accepts_nested_attributes_for,并且在show_params中应将其添加为quiz_attributes而不是answered_questions_attributes。尽管您使用show_params两次,但在一个模型上一次使用,而在另一个模型上一次使用,因此您可能需要两个参数。否则,您会破坏另一个。