-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed faulty nil checks #2139
base: master
Are you sure you want to change the base?
Fixed faulty nil checks #2139
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -455,8 +455,14 @@ def raw_score(scores) | |
end | ||
|
||
def grade | ||
@problem = @assessment.problems.find(params[:problem]) | ||
@submission = @assessment.submissions.find(params[:submission]) | ||
@problem = @assessment.problems.find_by(params[:problem]) | ||
if @problem.nil? | ||
flash[:error] = "Could not find problem #{params[:problem]}" | ||
end | ||
@submission = @assessment.submissions.find_by(params[:submission]) | ||
if @submission.nil? | ||
flash[:error] = "Could not find submission #{params[:submission]}" | ||
end | ||
Comment on lines
+460
to
+465
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting error messages in the flash is a good practice when handling nil values. However, after setting the error message, consider redirecting the user to a relevant page (e.g., the assessments index or a custom error page). This prevents the execution of further code that assumes if @problem.nil?
flash[:error] = "Could not find problem #{params[:problem]}"
redirect_to assessments_path and return
end
if @submission.nil?
flash[:error] = "Could not find submission #{params[:submission]}"
redirect_to assessments_path and return
end |
||
# Shows a form which has the submission on top, and feedback on bottom | ||
begin | ||
subFile = Rails.root.join("courses", @course.name, @assessment.name, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,23 +88,25 @@ def create | |
|
||
action_auth_level :show, :student | ||
def show | ||
@requestedUser = @cud.course.course_user_data.find(params[:id]) | ||
@requestedUser = @cud.course.course_user_data.find_by(id: params[:id]) | ||
if @requestedUser.nil? | ||
flash[:error] = "Could not find user #{params[:id]}" | ||
redirect_to([:users, @course]) && return | ||
end | ||
respond_to do |format| | ||
if @requestedUser | ||
format.html | ||
format.json { render json: @requestedUser.to_json } | ||
else | ||
format.json { head :bad_request } | ||
end | ||
end | ||
end | ||
|
||
action_auth_level :edit, :student | ||
def edit | ||
@editCUD = @course.course_user_data.find(params[:id]) | ||
@editCUD = @course.course_user_data.find_by(id: params[:id]) | ||
if @editCUD.nil? | ||
flash[:error] = "Can't find user in the course" | ||
redirect_to(action: "index") && return | ||
redirect_to(action: "show") && return | ||
end | ||
|
||
if (@editCUD.id != @cud.id) && !@cud.instructor? && | ||
|
@@ -124,7 +126,7 @@ def update | |
# ensure presence of nickname | ||
# isn't a User model validation since users can start off without nicknames | ||
# application_controller's authenticate_user redirects here if nickname isn't set | ||
@editCUD = @course.course_user_data.find(params[:id]) | ||
@editCUD = @course.course_user_data.find_by(id: params[:id]) | ||
redirect_to(action: "index") && return if @editCUD.nil? | ||
|
||
if @cud.student? | ||
|
@@ -174,7 +176,7 @@ def update | |
|
||
action_auth_level :destroy, :instructor | ||
def destroy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. destroy / destroyConfirm don't actually seem to be routes that are currently used, might be worth removing. |
||
@destroyCUD = @course.course_user_data.find(params[:id]) | ||
@destroyCUD = @course.course_user_data.find_by(id: params[:id]) | ||
if @destroyCUD && @destroyCUD != @cud && params[:yes1] && params[:yes2] && params[:yes3] | ||
@destroyCUD.destroy # awwww!!! | ||
end | ||
|
@@ -187,7 +189,7 @@ def destroy | |
# this GET page confirms that the instructor wants to destroy the user | ||
action_auth_level :destroyConfirm, :instructor | ||
def destroyConfirm | ||
@destroyCUD = @course.course_user_data.find(params[:id]) | ||
@destroyCUD = @course.course_user_data.find_by(id: params[:id]) | ||
return unless @destroyCUD.nil? | ||
|
||
flash[:error] = "The user to be deleted is not in the course" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
require File.expand_path(File.dirname(__FILE__)+'/../config/environment') | ||
|
||
course_id = ARGV[0] | ||
@course = Course.find(course_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this script is unused, should delete There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I delete the whole file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
@course = Course.find_by(id: course_id) | ||
exit if !@course | ||
|
||
#require(File.expand_path("app/models/gradebook_cache.rb")) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
find_by
improves nil handling, but the wayparams
are passed to it might not work as expected.find_by
expects key-value pairs, butparams[:problem]
andparams[:submission]
are likely to be single values (IDs). Consider specifying the column names explicitly, for example,find_by(id: params[:problem])
. This ensures clarity and prevents potential bugs if the parameters structure changes.Committable suggestion