diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index f363b9297..0afc9dfda 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -156,16 +156,24 @@ class ExerciseController < ApplicationController end if @exercise_questions.save # params[:exercise_choice] 标准答案参数 - standart_answer = ExerciseStandardAnswer.new - standart_answer.exercise_question_id = @exercise_questions.id - if @exercise_questions.question_type == 1 - standart_answer.exercise_choice_id = sigle_selection_standard_answer(params[:exercise_choice]) - elsif @exercise_questions.question_type == 2 - standart_answer.exercise_choice_id = multiselect_standard_answer(params[:exercise_choice]) + # 问答题标准答案有三个,单独处理 + if @exercise_questions.question_type == 3 + for i in 1..params[:exercise_choice].count + standart_answer = ExerciseStandardAnswer.new + standart_answer.exercise_question_id = @exercise_questions.id + standart_answer.answer_text = params[:exercise_choice].values[i-1] + standart_answer.save + end else - standart_answer.answer_text = fill_standart_answer(params[:exercise_choice]) + standart_answer = ExerciseStandardAnswer.new + standart_answer.exercise_question_id = @exercise_questions.id + if @exercise_questions.question_type == 1 + standart_answer.exercise_choice_id = sigle_selection_standard_answer(params[:exercise_choice]) + else + standart_answer.exercise_choice_id = multiselect_standard_answer(params[:exercise_choice]) + end + standart_answer.save end - standart_answer.save respond_to do |format| format.js end @@ -178,16 +186,17 @@ class ExerciseController < ApplicationController # params[:question_answer] eg:A、B、C选项 def update_exercise_question @exercise_question = ExerciseQuestion.find params[:exercise_question] - @exercise_question.question_title = params[:exercise_questions_title].nil? || params[:exercise_questions_title].empty? ? l(:label_enter_single_title) : params[:exercise_questions_title] + @exercise_question.question_title = params[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title] + @exercise_question.question_score = params[:question_score] ################处理选项 if params[:question_answer] - @exercise_question.exercise_answers.each do |answer| - answer.destroy unless params[:question_answer].keys.include? answer.id.to_s - end + # @exercise_question.exercise_choices.each do |answer| + # answer.destroy unless params[:question_answer].keys.include? answer.id.to_s + # end # 界面需要判断选择题至少有一个选项 - for i in 1..params[:question_answer].count - question = @exercise_question.exercise_answers.find_by_id params[:question_answer].keys[i-1] - answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1] + for i in 1..@exercise_question.exercise_choices.count + question = @exercise_question.exercise_choices.find_by_id params[:question_answer].keys[i-1] + # answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1] if question question.exercise_choices_id = i question.answer_text = answer diff --git a/app/helpers/exercise_helper.rb b/app/helpers/exercise_helper.rb index 3a7c0a00a..7f3d1862e 100644 --- a/app/helpers/exercise_helper.rb +++ b/app/helpers/exercise_helper.rb @@ -27,8 +27,11 @@ module ExerciseHelper answer.to_i end - def fill_standart_answer(params) - + def fill_standart_answer(params, standart_answer) + params.each do |param| + standart_answer.answer_text = param.value + standart_answer.save + end end #判断用户是否已经提交了问卷