From 115fca900985feb78b9e71fdae3984060d13e9ea Mon Sep 17 00:00:00 2001 From: Derek Montgomery Date: Mon, 1 Aug 2016 15:58:20 -0500 Subject: [PATCH] Summary edit 3 primary input types --- app/assets/javascripts/summary-edit.js | 25 +++++++++++++++++-------- app/controllers/candidate_controller.rb | 11 ++++------- app/views/candidate/summary.html.erb | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/summary-edit.js b/app/assets/javascripts/summary-edit.js index 0b4a764..183ea4f 100644 --- a/app/assets/javascripts/summary-edit.js +++ b/app/assets/javascripts/summary-edit.js @@ -93,7 +93,7 @@ var saveClickHandler = function(e) { var executeQuery; var questionId = thisEd.find('.button-edit').attr('data-questionid'); - if (thisEd.find('textarea.code-answer')) { + if (thisEd.hasClass('live_code-type')) { var htmlAnswer = $(thisEd.find('textarea.code-html')[0]).val(); var cssAnswer = $(thisEd.find('textarea.code-css')[0]).val(); var jsAnswer = $(thisEd.find('textarea.code-js')[0]).val(); @@ -102,29 +102,38 @@ var saveClickHandler = function(e) { 'css': cssAnswer, 'js': jsAnswer } - } else if(thisEd.find('input').attr('type')=='radio') { + } else if(thisEd.hasClass('radio-type')) { $(thisEd.find('input')).each(function() { if($(this).prop('checked')==true) { - data = $(this).val(); + data = ({ + 'radio': $(this).val() + }) } }); - } else if(thisEd.find('input').attr('type')=='checkbox') { + } else if(thisEd.hasClass('checkbox-type')) { + data = {'checkbox': []}; + $(thisEd.find('input')).each(function() { if($(this).prop('checked')==true) { - data.push($(this).val()); + data.checkbox.push($(this).val()); } }); } else { - data = thisEd.find('textarea').val(); + data = {'text': thisEd.find('textarea').val()}; } if(data == '') { $(thisEd).before('
Please select or enter a value.
'); } else { thisEd.find('textarea:not(.code-answer)').replaceWith('

' + $.trim(thisEd.find('textarea').val()) + '

'); + url = thisEd.closest('form').attr('action'); + $.ajax({ type: "POST", - url: "/summary", - data:{ id: questionId, answer: data}, + url: url, + data: ({ + 'answer': data, + 'submit': true + }), success: function(data){ executeQuery = true; //console.log(data); diff --git a/app/controllers/candidate_controller.rb b/app/controllers/candidate_controller.rb index 6390815..3855b53 100644 --- a/app/controllers/candidate_controller.rb +++ b/app/controllers/candidate_controller.rb @@ -22,7 +22,8 @@ class CandidateController < ApplicationController end def update_answer - qid = prep_status.current_question_id + qid = params[:qid] ||= prep_status.current_question_id + @answer = prep_answer qid send "process_#{prep_question(qid).input_type}" end @@ -68,8 +69,8 @@ class CandidateController < ApplicationController ) end - def prep_answer - answer_ids = { question_id: answer_params[:question_id], candidate_id: current_candidate.to_i } + def prep_answer qid = answer_params[:question_id] + answer_ids = { question_id: qid, candidate_id: current_candidate.to_i } answer = Answer.find_or_create_by(answer_ids) answer end @@ -89,7 +90,6 @@ class CandidateController < ApplicationController end def process_text - @answer = prep_answer @answer.update(answer: answer_params[:text], saved: params.key?(:save), submitted: params.key?(:submit)) @@ -97,7 +97,6 @@ class CandidateController < ApplicationController end def process_radio - @answer = prep_answer @answer.update(answer: answer_params[:radio], saved: params.key?(:save), submitted: params.key?(:submit)) @@ -105,7 +104,6 @@ class CandidateController < ApplicationController end def process_checkbox - @answer = prep_answer @answer.update(answer: answer_params[:checkbox], saved: params.key?(:save), submitted: params.key?(:submit)) @@ -113,7 +111,6 @@ class CandidateController < ApplicationController end def process_live_code - @answer = prep_answer @answer.update(answer: answer_params[:live_code].to_h, saved: params.key?(:save), submitted: params.key?(:submit)) diff --git a/app/views/candidate/summary.html.erb b/app/views/candidate/summary.html.erb index 5230789..4596e34 100644 --- a/app/views/candidate/summary.html.erb +++ b/app/views/candidate/summary.html.erb @@ -6,7 +6,7 @@

<% @quiz.each do |question| %> - <%= form_for(:answer, url: post_summary_path, html:{id: 'summary-form'}) do |form| %> + <%= form_for(:answer, url: post_answer_path(answer_id: question.answer_id, qid: question.question_id), html:{class: 'summary-form'}) do |form| %>