diff --git a/app/views/quiz/_radio.html.erb b/app/views/quiz/_radio.html.erb
index c4ade59..ebd2db1 100644
--- a/app/views/quiz/_radio.html.erb
+++ b/app/views/quiz/_radio.html.erb
@@ -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 | %>
<%
option_id = "#{question.question_id}#{sanitize_to_id(option.value)}"
- checked = answer == option.value ? 'checked' : ''
+ checked = answer_string == option.value ? 'checked' : ''
%>
diff --git a/app/views/quiz/_radio_other.html.erb b/app/views/quiz/_radio_other.html.erb
index 086a8c9..af22dd6 100644
--- a/app/views/quiz/_radio_other.html.erb
+++ b/app/views/quiz/_radio_other.html.erb
@@ -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']
question.input_options.each do | option |
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' : '' }}
%>
- <%= 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) %>
<%
@@ -16,10 +16,10 @@
<%
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 }}
%>
- <%= 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' %>
<%= text_field_tag 'answer[answer_hash][other]', other_value, text_html %>
diff --git a/app/views/quiz/_text.html.erb b/app/views/quiz/_text.html.erb
index 359f3ff..11a0cd0 100644
--- a/app/views/quiz/_text.html.erb
+++ b/app/views/quiz/_text.html.erb
@@ -1,7 +1,7 @@
-<% answer = answer.respond_to?(:answer) ? answer.answer : answer %>
+<% answer_string = answer.respond_to?(:answer) ? answer.answer : answer %>
-<%= text_area_tag 'answer[answer]', answer, {rows: 10, data: { last: answer } } %>
+<%= text_area_tag 'answer[answer]', answer_string, {rows: 10, data: { last: answer_string } } %>
Characters remaining:
diff --git a/test/integration/question_error_test.rb b/test/integration/question_error_test.rb
new file mode 100644
index 0000000..4525962
--- /dev/null
+++ b/test/integration/question_error_test.rb
@@ -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