多选题答题功能

This commit is contained in:
z9hang 2015-01-15 11:54:30 +08:00
parent d2fadfda08
commit d684acf527
3 changed files with 89 additions and 40 deletions

View File

@ -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

View File

@ -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

View File

@ -34,7 +34,7 @@
</div>
<div class="cl"></div>
<div class="ur_inputs">
<%= form_tag(commit_answer_poll_path(@poll),:remote => true,:id => 'form_'+ pq.id.to_s) do %>
<form>
<table class="ur_table" >
<tbody>
<% pq.poll_answers.each do |pa| %>
@ -57,7 +57,7 @@
});
}
</script>
<%= 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 %>
</label>
</td>
@ -65,7 +65,7 @@
<% end %>
</tbody>
</table>
<% end %>
</form>
</div>
</li>
<% elsif pq.question_type == 2 %>
@ -80,20 +80,45 @@
</div>
<div class="cl"></div>
<div class="ur_inputs">
<table class="ur_table" >
<tbody>
<% pq.poll_answers.each do |pa| %>
<tr>
<td>
<label >
<input class="ur_checkbox" type="checkbox" value="新建选项" >
<%= pa.answer_text %>
</label>
</td>
</tr>
<% end %>
</tbody>
</table>
<form>
<table class="ur_table" >
<tbody>
<% pq.poll_answers.each do |pa| %>
<tr>
<td>
<label >
<script>
function click_<%= pa.id %>(obj)
{
$.ajax({
type: "post",
url: "<%= commit_answer_poll_path(@poll) %>",
data: {
poll_answer_id: <%= pa.id %>,
poll_question_id: <%= pq.id %>
},
success: function (data) {
if(data == "true")
{
obj.checked = true;
}
else
{
obj.checked = false;
}
}
});
}
</script>
<input class="ur_checkbox" type="checkbox" onclick="click_<%= pa.id %>(this);return false;" <%= answer_be_selected?(pa,User.current) ? "checked":"" %>>
<%= pa.answer_text %>
</label>
</td>
</tr>
<% end %>
</tbody>
</table>
</form>
</div>
</li>
<% elsif pq.question_type == 3 %>