多选题答题功能

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 def commit_answer
@pv = get_poll_vote(User.current.id,params[:poll_question_id]) pq = PollQuestion.find(params[:poll_question_id])
if params[:poll_answer_id] if pq.question_type == 1
@pv.poll_answer_id = params[:poll_answer_id] #单选题
end pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id)
if params[:vote_text] if pv.nil?
@pv.vote_text = params[:vote_text] pv = PollVote.new
end pv.user_id = User.current.id
@pv_saved = false pv.poll_question_id = params[:poll_question_id]
if @pv.save end
@pv_saved = true 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 end
#respond_to do |format| #respond_to do |format|
# format.js # format.js
# format.json # format.json
#end #end
render :text => "ok"
end end
private private

View File

@ -18,18 +18,13 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module PollHelper module PollHelper
def get_poll_vote(user_id,poll_question_id) #判断选项是否被选中
pq = PollQuestion.find(poll_question_id) def answer_be_selected?(answer,user)
if pq.question_type == 1 pv = answer.poll_votes.where("#{PollVote.table_name}.user_id = #{user.id} ")
pv = PollVote.find_by_poll_question_id_and_user_id(poll_question_id,user_id) if !pv.nil? && pv.count > 0
if pv.nil? true
pv = PollVote.new else
pv.user_id = user_id false
pv.poll_question_id = poll_question_id
pv
else
pv
end
end end
end end
end end

View File

@ -34,7 +34,7 @@
</div> </div>
<div class="cl"></div> <div class="cl"></div>
<div class="ur_inputs"> <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" > <table class="ur_table" >
<tbody> <tbody>
<% pq.poll_answers.each do |pa| %> <% pq.poll_answers.each do |pa| %>
@ -57,7 +57,7 @@
}); });
} }
</script> </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 %> <%= pa.answer_text %>
</label> </label>
</td> </td>
@ -65,7 +65,7 @@
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<% end %> </form>
</div> </div>
</li> </li>
<% elsif pq.question_type == 2 %> <% elsif pq.question_type == 2 %>
@ -80,20 +80,45 @@
</div> </div>
<div class="cl"></div> <div class="cl"></div>
<div class="ur_inputs"> <div class="ur_inputs">
<table class="ur_table" > <form>
<tbody> <table class="ur_table" >
<% pq.poll_answers.each do |pa| %> <tbody>
<tr> <% pq.poll_answers.each do |pa| %>
<td> <tr>
<label > <td>
<input class="ur_checkbox" type="checkbox" value="新建选项" > <label >
<%= pa.answer_text %> <script>
</label> function click_<%= pa.id %>(obj)
</td> {
</tr> $.ajax({
<% end %> type: "post",
</tbody> url: "<%= commit_answer_poll_path(@poll) %>",
</table> 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> </div>
</li> </li>
<% elsif pq.question_type == 3 %> <% elsif pq.question_type == 3 %>