Skip to content

Commit 4107b4d

Browse files
authored
Fix Flash links (#1549)
1 parent 462f491 commit 4107b4d

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

app/controllers/assessment/autograde.rb

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ def regradeBatch
9898
end
9999
end
100100

101-
if failed_list.length > 0
102-
flash[:error] = "Warning: Could not regrade #{failed_list.length} submission(s):<br>"
101+
failure_jobs = failed_list.length
102+
if failure_jobs > 0
103+
flash[:error] = "Warning: Could not regrade #{pluralize(failure_jobs, "submission")}:<br>"
103104
failed_list.each do |failure|
104105
if failure[:error].error_code == :nil_submission
105106
flash[:error] += "Unrecognized submission ID<br>"
@@ -109,12 +110,15 @@ def regradeBatch
109110
end
110111
end
111112

112-
success_jobs = submission_ids.size - failed_list.length
113+
success_jobs = submission_ids.size - failure_jobs
113114
if success_jobs > 0
114-
link = "<a href=\"#{url_for(controller: 'jobs')}\">#{success_jobs} submission</a>"
115-
flash[:success] = ("Regrading #{link}").html_safe
115+
link = "<a href=\"#{url_for(controller: 'jobs')}\">#{pluralize(success_jobs, "submission")}</a>"
116+
flash[:success] = ("Regrading #{link}")
116117
end
117118

119+
# For both :success and :error
120+
flash[:html_safe] = true
121+
118122
redirect_to([@course, @assessment, :submissions]) && return
119123
end
120124

@@ -141,8 +145,9 @@ def regradeAll
141145
end
142146
end
143147

144-
if failed_list.length > 0
145-
flash[:error] = "Warning: Could not regrade #{failed_list.length} submission(s):<br>"
148+
failure_jobs = failed_list.length
149+
if failure_jobs > 0
150+
flash[:error] = "Warning: Could not regrade #{pluralize(failure_jobs, "submission")}:<br>"
146151
failed_list.each do |failure|
147152
if failure[:error].error_code == :nil_submission
148153
flash[:error] += "Unrecognized submission ID<br>"
@@ -152,12 +157,15 @@ def regradeAll
152157
end
153158
end
154159

155-
success_jobs = last_submissions.size - failed_list.length
160+
success_jobs = last_submissions.size - failure_jobs
156161
if success_jobs > 0
157-
link = "<a href=\"#{url_for(controller: 'jobs')}\">#{success_jobs} students</a>"
158-
flash[:success] = ("Regrading the most recent submissions from #{link}").html_safe
162+
link = "<a href=\"#{url_for(controller: 'jobs')}\">#{pluralize(success_jobs, "student")}</a>"
163+
flash[:success] = ("Regrading the most recent submissions from #{link}")
159164
end
160165

166+
# For both :success and :error
167+
flash[:html_safe] = true
168+
161169
redirect_to([@course, @assessment, :submissions]) && return
162170
end
163171

@@ -186,6 +194,7 @@ def sendJob_AddHTMLMessages(course, assessment, submissions)
186194
if @cud.instructor?
187195
link = (view_context.link_to "Autograder Settings", [:edit, course, assessment, :autograder])
188196
flash[:error] += " Visit #{link} to set the autograding properties."
197+
flash[:html_safe] = true
189198
else
190199
flash[:error] += " Please contact your instructor."
191200
end
@@ -199,6 +208,7 @@ def sendJob_AddHTMLMessages(course, assessment, submissions)
199208
if @cud.instructor?
200209
link = (view_context.link_to "Autograder Settings", [:edit, course, assessment, :autograder])
201210
flash[:error] += " (Verify the autograding properties at #{link}.)\nErrorMsg: " + e.additional_data
211+
flash[:html_safe] = true
202212
end
203213
when :missing_autograder_file
204214
flash[:error] = "One or more files are missing in the server. Please contact the instructor. The missing files are: " + e.additional_data
@@ -211,8 +221,8 @@ def sendJob_AddHTMLMessages(course, assessment, submissions)
211221

212222
link = "<a href=\"#{url_for(controller: 'jobs', action: 'getjob', id: job)}\">Job ID = #{job}</a>"
213223
flash[:success] = ("Submitted file #{submissions[0].filename} (#{link}) for autograding." \
214-
" Refresh the page to see the results.").html_safe
215-
224+
" Refresh the page to see the results.")
225+
flash[:html_safe] = true
216226
job
217227
end
218228

app/controllers/assessment/grading.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def parse_csv(csv, data_type)
164164
rescue CSV::MalformedCSVError => e
165165
flash[:error] = "Failed to parse CSV -- make sure the grades " \
166166
"are formatted correctly: <pre>#{e}</pre>"
167+
flash[:html_safe] = true
167168
return false, []
168169
end
169170

app/views/layouts/application.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,18 @@
6868

6969
<!-- Flashes -->
7070
<div id="flashes">
71-
<!-- Ruby escapes msg by default, no need for sanitization -->
71+
<%# msg is escaped by default %>
72+
<%# if you must output raw html, set flash[:html_safe] = true in controller %>
73+
<% html_safe = flash[:html_safe] %>
74+
<% flash.delete(:html_safe) %>
7275
<% flash.each do |name, msg| %>
7376
<% if name == "roster_error" %>
7477
<div class="error" id="flash_error">
7578
<%= render :partial=>'courses/uploadError',
7679
locals: {roster_errors: msg} %>
7780
</div>
7881
<% else %>
82+
<% msg = msg.html_safe if html_safe %>
7983
<%= content_tag :div, msg, id: "flash_#{name}",
8084
class: "#{name}" %>
8185
<% end %>

app/views/layouts/home.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@
4545

4646
<!-- Flashes -->
4747
<div id="flashes">
48-
<!-- Ruby escapes msg by default, no need for sanitization -->
48+
<%# msg is escaped by default %>
49+
<%# if you must output raw html, set flash[:html_safe] = true in controller %>
50+
<% html_safe = flash[:html_safe] %>
51+
<% flash.delete(:html_safe) %>
4952
<% flash.each do |name, msg| %>
53+
<% msg = msg.html_safe if html_safe %>
5054
<%= content_tag :div, msg, id: "flash_#{name}",
5155
class: "#{name}" %>
5256
<% end %>

0 commit comments

Comments
 (0)