百分比除数为0问题
This commit is contained in:
parent
b8047bec28
commit
dd42c89ee6
|
@ -178,20 +178,26 @@ class PollController < ApplicationController
|
||||||
#单选题
|
#单选题
|
||||||
pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id)
|
pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id)
|
||||||
if pv.nil?
|
if pv.nil?
|
||||||
|
#尚未答该题,添加答案
|
||||||
pv = PollVote.new
|
pv = PollVote.new
|
||||||
pv.user_id = User.current.id
|
pv.user_id = User.current.id
|
||||||
pv.poll_question_id = params[:poll_question_id]
|
pv.poll_question_id = params[:poll_question_id]
|
||||||
end
|
end
|
||||||
|
#修改该题对应答案
|
||||||
pv.poll_answer_id = params[:poll_answer_id]
|
pv.poll_answer_id = params[:poll_answer_id]
|
||||||
if pv.save
|
if pv.save
|
||||||
|
#保存成功返回成功信息及当前以答题百分比
|
||||||
@percent = get_percent(@poll,User.current)
|
@percent = get_percent(@poll,User.current)
|
||||||
render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)}
|
render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)}
|
||||||
else
|
else
|
||||||
|
#返回失败信息
|
||||||
render :json => {:text => "failure"}
|
render :json => {:text => "failure"}
|
||||||
end
|
end
|
||||||
elsif pq.question_type == 2
|
elsif pq.question_type == 2
|
||||||
|
#多选题
|
||||||
pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id)
|
pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id)
|
||||||
if pv.nil?
|
if pv.nil?
|
||||||
|
#尚未答该题,添加答案
|
||||||
pv = PollVote.new
|
pv = PollVote.new
|
||||||
pv.user_id = User.current.id
|
pv.user_id = User.current.id
|
||||||
pv.poll_question_id = params[:poll_question_id]
|
pv.poll_question_id = params[:poll_question_id]
|
||||||
|
@ -203,6 +209,7 @@ class PollController < ApplicationController
|
||||||
render :json => {:text => "failure"}
|
render :json => {:text => "failure"}
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
#pv不为空,则当前选项之前已被选择,再次点击则是不再选择该项,故删除该答案
|
||||||
if pv.delete
|
if pv.delete
|
||||||
@percent = get_percent(@poll,User.current)
|
@percent = get_percent(@poll,User.current)
|
||||||
render :json => {:text => "false" ,:percent => format("%.2f" ,@percent)}
|
render :json => {:text => "false" ,:percent => format("%.2f" ,@percent)}
|
||||||
|
@ -211,12 +218,16 @@ class PollController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elsif pq.question_type == 3 || pq.question_type == 4
|
elsif pq.question_type == 3 || pq.question_type == 4
|
||||||
|
#单行文本,多行文本题
|
||||||
pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id)
|
pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id)
|
||||||
if pv.nil?
|
if pv.nil?
|
||||||
|
#pv为空之前尚未答题,添加答案
|
||||||
if params[:vote_text].nil? || params[:vote_text].blank?
|
if params[:vote_text].nil? || params[:vote_text].blank?
|
||||||
|
#用户提交空答案,视作不作答
|
||||||
@percent = get_percent(@poll,User.current)
|
@percent = get_percent(@poll,User.current)
|
||||||
render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
|
render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
|
||||||
else
|
else
|
||||||
|
#添加答案
|
||||||
pv = PollVote.new
|
pv = PollVote.new
|
||||||
pv.user_id = User.current.id
|
pv.user_id = User.current.id
|
||||||
pv.poll_question_id = params[:poll_question_id]
|
pv.poll_question_id = params[:poll_question_id]
|
||||||
|
@ -229,7 +240,9 @@ class PollController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
#pv不为空说明用户之前已作答
|
||||||
if params[:vote_text].nil? || params[:vote_text].blank?
|
if params[:vote_text].nil? || params[:vote_text].blank?
|
||||||
|
#用户提交空答案,视为删除答案
|
||||||
if pv.delete
|
if pv.delete
|
||||||
@percent = get_percent(@poll,User.current)
|
@percent = get_percent(@poll,User.current)
|
||||||
render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
|
render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
|
||||||
|
@ -237,6 +250,7 @@ class PollController < ApplicationController
|
||||||
render :json => {:text => "failure"}
|
render :json => {:text => "failure"}
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
#用户修改答案
|
||||||
pv.vote_text = params[:vote_text]
|
pv.vote_text = params[:vote_text]
|
||||||
if pv.save
|
if pv.save
|
||||||
@percent = get_percent(@poll,User.current)
|
@percent = get_percent(@poll,User.current)
|
||||||
|
@ -335,7 +349,11 @@ class PollController < ApplicationController
|
||||||
|
|
||||||
def get_percent poll,user
|
def get_percent poll,user
|
||||||
complete_count = get_complete_question(poll,user).count
|
complete_count = get_complete_question(poll,user).count
|
||||||
(complete_count.to_f / poll.poll_questions.count.to_f)*100
|
if poll.poll_questions.count == 0
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return (complete_count.to_f / poll.poll_questions.count.to_f)*100
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#PollUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个
|
#PollUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个
|
||||||
|
|
Loading…
Reference in New Issue