From 13557bb96f20c1897555a82d310c3a96c819132b Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 24 Jul 2019 08:47:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E8=80=83=E7=AD=94=E6=A1=88=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E4=B8=8D=E8=83=BD=E4=B8=BA=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/challenges_controller.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index 9b5f0b869..fdaa30305 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -230,11 +230,16 @@ class ChallengesController < ApplicationController def crud_answer raise '参考答案不能为空' if params[:challenge_answer].empty? raise '占比之和必须为100%' if params[:challenge_answer].map{|a| a[:score]}.sum != 100 - @challenge.challenge_answers.destroy_all if @challenge.challenge_answers - params[:challenge_answer].each_with_index do |answer, index| - ChallengeAnswer.create(name: answer[:name], contents: answer[:contents], - level: index+1, score: answer[:score], challenge_id: @challenge.id) + ActiveRecord::Base.transaction do + @challenge.challenge_answers.destroy_all if @challenge.challenge_answers + params[:challenge_answer].each_with_index do |answer, index| + # 内容为空不保存 + next if answer[:contents].blank? + ChallengeAnswer.create(name: answer[:name], contents: answer[:contents], + level: index+1, score: answer[:score], challenge_id: @challenge.id) + end end + end # 查看参考答案接口