hotfix: ensure questions get proper error messages

This commit is contained in:
Mark Moser 2017-04-17 16:41:41 -05:00
commit 9f43646eaa
4 changed files with 79 additions and 9 deletions

View File

@ -1,8 +1,8 @@
<% answer = answer.try(:answer) || answer %> <% answer_string = answer.try(:answer) || answer %>
<%= form.collection_radio_buttons(:answer, question.input_options, :to_s, :to_s, {}, {class: 'radio'}) do | option | %> <%= form.collection_radio_buttons(:answer, question.input_options, :to_s, :to_s, {}, {class: 'radio'}) do | option | %>
<% <%
option_id = "#{question.question_id}#{sanitize_to_id(option.value)}" option_id = "#{question.question_id}#{sanitize_to_id(option.value)}"
checked = answer == option.value ? 'checked' : '' checked = answer_string == option.value ? 'checked' : ''
%> %>
<div class="form-group-multiples"> <div class="form-group-multiples">

View File

@ -1,13 +1,13 @@
<% <%
answer = question.answer.nil? ? '' : Array(question.answer['options']).first answer_string = question.answer.nil? ? '' : Array(question.answer['options']).first
other_value = question.answer.nil? ? '' : question.answer['other'] other_value = question.answer.nil? ? '' : question.answer['other']
question.input_options.each do | option | question.input_options.each do | option |
option_id = "#{option.parameterize}_#{question.to_i}" option_id = "#{option.parameterize}_#{question.to_i}"
radio_html = {class: 'radio', id: option_id, data: {last: (answer == option) ? 'checked' : '' }} radio_html = {class: 'radio', id: option_id, data: {last: (answer_string == option) ? 'checked' : '' }}
%> %>
<div class="form-group-multiples"> <div class="form-group-multiples">
<%= radio_button_tag('answer[answer_hash][options][]', option, (answer == option), radio_html) %> <%= radio_button_tag('answer[answer_hash][options][]', option, (answer_string == option), radio_html) %>
<%= label_tag(option_id, option) %> <%= label_tag(option_id, option) %>
</div> </div>
<% <%
@ -16,10 +16,10 @@
<div class="form-group-multiples"> <div class="form-group-multiples">
<% <%
option_id = "other_#{question.to_i}" option_id = "other_#{question.to_i}"
radio_html = {class: 'radio', id: option_id, data: { last: answer }} radio_html = {class: 'radio', id: option_id, data: { last: answer_string }}
text_html = {class: 'input-other', id: "text_#{option_id}", data: { last: other_value }} text_html = {class: 'input-other', id: "text_#{option_id}", data: { last: other_value }}
%> %>
<%= radio_button_tag('answer[answer_hash][options][]', 'other', (answer == 'other'), radio_html) %> <%= radio_button_tag('answer[answer_hash][options][]', 'other', (answer_string == 'other'), radio_html) %>
<%= label_tag option_id, 'Other' %> <%= label_tag option_id, 'Other' %>
<%= text_field_tag 'answer[answer_hash][other]', other_value, text_html %> <%= text_field_tag 'answer[answer_hash][other]', other_value, text_html %>
</div> </div>

View File

@ -1,7 +1,7 @@
<% answer = answer.respond_to?(:answer) ? answer.answer : answer %> <% answer_string = answer.respond_to?(:answer) ? answer.answer : answer %>
<label for="answer_answer">Enter answer here</label> <label for="answer_answer">Enter answer here</label>
<%= text_area_tag 'answer[answer]', answer, {rows: 10, data: { last: answer } } %> <%= text_area_tag 'answer[answer]', answer_string, {rows: 10, data: { last: answer_string } } %>
<div class="chars <%= params[:action] == 'summary' ? 'hidden' : '' %>">Characters remaining: <span></span></div> <div class="chars <%= params[:action] == 'summary' ? 'hidden' : '' %>">Characters remaining: <span></span></div>

View File

@ -0,0 +1,70 @@
# frozen_string_literal: true
require 'test_helper'
class QuestionErrorTest < ActionDispatch::IntegrationTest
test "testing full quiz for question error messages" do
auth_candidate candidates(:gillian)
# text
post post_answer_path, params: { answer: { question_id: questions(:fed1).id } }
assert_select 'h2', questions(:fed1).question
assert_select '.error', /an answer/
post post_answer_path, params: { answer: { question_id: questions(:fed1).id, answer: "fooBarBaz" } }
# checkbox
post post_answer_path, params: { answer: { question_id: questions(:fed2).id } }
assert_select 'h2', questions(:fed2).question
assert_select '.error', /an answer/
post post_answer_path, params: { answer: { question_id: questions(:fed2).id, answer: "fooBarBaz" } }
# live_code
post post_answer_path, params: { answer: { question_id: questions(:fed3).id } }
assert_select 'h2', questions(:fed3).question
assert_select '.error', /comments or code/
post post_answer_path, params: { answer: {
question_id: questions(:fed3).id, answer_hash: { html: "fooBarBaz" }
} }
# text
post post_answer_path, params: { answer: { question_id: questions(:fed4).id, answer: "fooBarBaz" } }
# radio
post post_answer_path, params: { answer: { question_id: questions(:fed5).id } }
assert_select 'h2', questions(:fed5).question
assert_select '.error', /an answer/
post post_answer_path, params: { answer: { question_id: questions(:fed5).id, answer: "fooBarBaz" } }
# text
post post_answer_path, params: { answer: { question_id: questions(:fed6).id, answer: "fooBarBaz" } }
# live_code
post post_answer_path, params: { answer: {
question_id: questions(:fed7).id, answer_hash: { html: "fooBarBaz" }
} }
# radio_other
post post_answer_path, params: { answer: { question_id: questions(:fed8).id } }
assert_select 'h2', questions(:fed8).question
assert_select '.error', /an answer/
post post_answer_path, params: { answer: {
question_id: questions(:fed8).id, answer_hash: { options: "fooBarBaz" }
} }
# checkbox_other
post post_answer_path, params: { answer: { question_id: questions(:fed9).id } }
assert_select 'h2', questions(:fed9).question
assert_select '.error', /an answer/
post post_answer_path, params: { answer: {
question_id: questions(:fed9).id, answer_hash: { options: "fooBarBaz" }
} }
# checkbox
post post_answer_path, params: { answer: { question_id: questions(:fed10).id, answer: "fooBarBaz" } }
# checkbox
post post_answer_path, params: { answer: { question_id: questions(:fed11).id, answer: "fooBarBaz" } }
# checkbox
post post_answer_path, params: { answer: { question_id: questions(:fed12).id, answer: "fooBarBaz" } }
end
end