Ruby on rails 未知属性';回答了"问题"和"属性"x27;
我试图创建一个表单,用户可以在其中回答问题并提交到结果页面,但是,当我试图通过测验控制器传递AnsweredQuestions属性时,它告诉我这是一个“未知属性” 我收到错误:AnsweredQuestion的未知属性“已回答的问题”属性 quick.rb:Ruby on rails 未知属性';回答了"问题"和"属性"x27;,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我试图创建一个表单,用户可以在其中回答问题并提交到结果页面,但是,当我试图通过测验控制器传递AnsweredQuestions属性时,它告诉我这是一个“未知属性” 我收到错误:AnsweredQuestion的未知属性“已回答的问题”属性 quick.rb: class Quiz < ApplicationRecord validates :title, presence: true, length: { maximum: 50 } has_many :questions, de
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
show.html.erb(在测验中):
@quick.id%>
问题%id>
@quick.id%>
更新:
show.html.erb(测验):
@quick.id%>
问题%id>
@quick.id%>
回答问题控制员:
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
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
类回答问题控制器
看起来您正在接受测验的嵌套属性,但使用回答问题
中的嵌套属性,而不是测验
中的嵌套属性。您需要在另一个模型中添加接受
的
嵌套属性,并在显示参数
中,它应该是测验属性
,而不是回答的问题属性
。尽管您使用了两次show_params
,一次用于一个模型,另一次用于另一个模型,因此您可能需要两个参数。否则你会打断另一个。我也用表格更新了原始问题,不知道你的意思是什么?我只是尝试在测验控制器当前用户中传递回答的问题参数。回答的问题。生成(显示参数)
我尝试改变它,使其用于测验而不是回答的问题,但它仍然不起作用。为什么您试图通过测验为回答的问题保存记录?这种逻辑应该放在它自己的控制器中。此外,关系设置不正确,测验
有许多问题
有许多问题回答
,因此问题回答
不应直接属于测验
,这导致了非规范化。我最初将其放在它自己的控制器中,但当我这样做时,它会说“已回答的问题”参数为空,我已更新帖子以反映它的外观,现在我已将其放在它自己的控制器中,我没有看到任何错误。。。您能否将调用answered_params
方法的结果发布到创建操作中?
<%= 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 %>
<%= 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 %>
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