From d684acf5273c038cba31ac51c412e1cc93015952 Mon Sep 17 00:00:00 2001 From: z9hang Date: Thu, 15 Jan 2015 11:54:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E9=80=89=E9=A2=98=E7=AD=94=E9=A2=98?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/poll_controller.rb | 51 ++++++++++++++++++++------ app/helpers/poll_helper.rb | 19 ++++------ app/views/poll/show.html.erb | 59 +++++++++++++++++++++--------- 3 files changed, 89 insertions(+), 40 deletions(-) diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index f83ff7cb9..1f1f67ca9 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -112,22 +112,51 @@ class PollController < ApplicationController #提交答案 def commit_answer - @pv = get_poll_vote(User.current.id,params[:poll_question_id]) - if params[:poll_answer_id] - @pv.poll_answer_id = params[:poll_answer_id] - end - if params[:vote_text] - @pv.vote_text = params[:vote_text] - end - @pv_saved = false - if @pv.save - @pv_saved = true + pq = PollQuestion.find(params[:poll_question_id]) + if pq.question_type == 1 + #单选题 + pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id) + if pv.nil? + pv = PollVote.new + pv.user_id = User.current.id + pv.poll_question_id = params[:poll_question_id] + end + pv.poll_answer_id = params[:poll_answer_id] + if pv.save + render :text => "ok" + else + render :text => "failure" + end + elsif pq.question_type == 2 + pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id) + if pv.nil? + pv = PollVote.new + pv.user_id = User.current.id + pv.poll_question_id = params[:poll_question_id] + pv.poll_answer_id = params[:poll_answer_id] + if pv.save + render :text => "true" + else + render :text => "failure" + end + else + if pv.delete + render :text => "false" + else + render :text => "failure" + end + end + elsif pq.question_type == 3 + elsif pq.question_type == 4 + end + + #respond_to do |format| # format.js # format.json #end - render :text => "ok" + end private diff --git a/app/helpers/poll_helper.rb b/app/helpers/poll_helper.rb index 202f33eaa..d612cbb97 100644 --- a/app/helpers/poll_helper.rb +++ b/app/helpers/poll_helper.rb @@ -18,18 +18,13 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module PollHelper - def get_poll_vote(user_id,poll_question_id) - pq = PollQuestion.find(poll_question_id) - if pq.question_type == 1 - pv = PollVote.find_by_poll_question_id_and_user_id(poll_question_id,user_id) - if pv.nil? - pv = PollVote.new - pv.user_id = user_id - pv.poll_question_id = poll_question_id - pv - else - pv - end + #判断选项是否被选中 + def answer_be_selected?(answer,user) + pv = answer.poll_votes.where("#{PollVote.table_name}.user_id = #{user.id} ") + if !pv.nil? && pv.count > 0 + true + else + false end end end \ No newline at end of file diff --git a/app/views/poll/show.html.erb b/app/views/poll/show.html.erb index 86815f80a..2dadbd2d3 100644 --- a/app/views/poll/show.html.erb +++ b/app/views/poll/show.html.erb @@ -34,7 +34,7 @@
- <%= form_tag(commit_answer_poll_path(@poll),:remote => true,:id => 'form_'+ pq.id.to_s) do %> +
<% pq.poll_answers.each do |pa| %> @@ -57,7 +57,7 @@ }); } - <%= radio_button "poll_vote","poll_answer_id",pa.id,:class=>"ur_radio",:onclick =>"click_#{pa.id}(this);return false;" %> + <%= radio_button "poll_vote","poll_answer_id",pa.id,:class=>"ur_radio",:onclick =>"click_#{pa.id}(this);return false;",:checked => answer_be_selected?(pa,User.current) %> <%= pa.answer_text %> @@ -65,7 +65,7 @@ <% end %>
- <% end %> +
<% elsif pq.question_type == 2 %> @@ -80,20 +80,45 @@
- - - <% pq.poll_answers.each do |pa| %> - - - - <% end %> - -
- -
+
+ + + <% pq.poll_answers.each do |pa| %> + + + + <% end %> + +
+ +
+
<% elsif pq.question_type == 3 %>