我正在处理课程申请。每门课程都有一个章节,每个章节都有一个测验。对于测验,我希望用户发布多项选择题的答案(见截图)
但是,我无法弄清楚我应该将此表单发布到哪个控制器操作方法。问题显示在 question#show 操作方法中,因此用户应该将问题的答案发布到问题控制器,但接下来是哪个操作?还是应该将其发布到 AnswersController?如果是这样,采取什么行动?这是一个帖子,但他并没有创建一个动作,对吧?
正如您所看到的,我对如何使“用户回答问题”场景尽可能平静感到有些困惑。
我的问题模型如下所示:
class Question < ActiveRecord::Base
belongs_to :quiz
has_many :answers, dependent: :delete_all
validates_presence_of :title
def self.subsequent_question(previous_question)
where("id > ?", previous_question).first
end
def self.is_last?(question_id)
question = Question.find(question_id)
last_question = Question.last
if question.id == last_question.id
return true
else
return false
end
end
end
问题控制器:
class QuestionsController < ApplicationController
def show
if Question.is_last?(params[:id])
question = Question.find(params[:id])
redirect_to quiz_complete_path(id: question.quiz_id)
else
@question = Question.subsequent_question(params[:id])
@quiz = Quiz.find(params[:quiz_id])
@chapter = Chapter.find(@quiz.chapter_id)
@course = Course.find(@chapter.course_id)
end
end
end
答案模型:
class Answer < ActiveRecord::Base
belongs_to :question
validates_presence_of :title
validates_uniqueness_of :is_correct, conditions: -> { where(is_correct: true) }
end
答案控制器:
class AnswersController < ApplicationController
def check_answer
@answer = Answer.find(params[:id])
question = Question.find(@answer.question_id)
if Question.is_last?(question.id)
redirect_to quiz_complete_path(id: question.quiz_id)
else
next_question = Question.subsequent_question(question.id)
redirect_to show_question_path(id: question.quiz_id, question_id: next_question.id)
end
end
end
谢谢你的帮助,
安东尼