我正在尝试创建一个表单,用户可以在其中回答问题并提交到结果页面,然而,当我试图通过测验控制器传递AnsweredQuestions属性时,它告诉我这是一个“未知属性”。
接收错误: 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
endAnsweredQuestion.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
endshow.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发布于 2018-08-07 21:04:22
看起来您正在接受Quiz的嵌套属性,但是在AnsweredQuestion中使用嵌套属性,而不是在Quiz中。您需要在另一个模型中添加accepts_nested_attributes_for,并且在show_params中它应该是quiz_attributes而不是answered_questions_attributes。尽管您正在使用show_params两次,一次在一个模型上,第二个在另一个模型上,因此您可能需要两个参数。否则你会弄坏另一个。
https://stackoverflow.com/questions/51726998
复制相似问题