Avoid error if no submission is present (on first load of editor)

This commit is contained in:
Jan Renz
2015-04-21 10:52:22 +02:00
parent c7583f3eda
commit 2cd474ceb0
2 changed files with 23 additions and 19 deletions

View File

@ -16,27 +16,31 @@ class CommentsController < ApplicationController
#if admin, show all comments.
#check whether user is the author of the passed file_id, if so, show all comments. otherwise, only show comments of auther and own comments
file = CodeOcean::File.find(params[:file_id])
submission = Submission.find(file.context_id)
#there might be no submission yet, so dont use find
submission = Submission.find_by(id: file.context_id)
if submission
is_admin = false
if current_user.respond_to? :external_id
user_id = current_user.external_id
else
user_id = current_user.id
is_admin = current_user.role == 'admin'
end
is_admin = false
if current_user.respond_to? :external_id
user_id = current_user.external_id
if(is_admin || user_id == submission.user_id)
# fetch all comments for this file
@comments = Comment.where(file_id: params[:file_id])
else
@comments = Comment.where(file_id: params[:file_id], user_id: user_id)
end
#@comments = Comment.where(file_id: params[:file_id])
#add names to comments
@comments.map{|comment| comment.username = Xikolo::UserClient.get(comment.user_id.to_s)[:display_name]}
else
user_id = current_user.id
is_admin = current_user.role == 'admin'
@comments = Comment.where(file_id: -1) #we need an empty relation here
end
if(is_admin || user_id == submission.user_id)
# fetch all comments for this file
@comments = Comment.where(file_id: params[:file_id])
else
@comments = Comment.where(file_id: params[:file_id], user_id: user_id)
end
#@comments = Comment.where(file_id: params[:file_id])
#add names to comments
@comments.map{|comment| comment.username = Xikolo::UserClient.get(comment.user_id.to_s)[:display_name]}
authorize!
end

View File

@ -28,7 +28,7 @@ class SubmissionsController < ApplicationController
# copy each annotation and set the target_file.id
unless(params[:annotations_arr].nil?)
params[:annotations_arr].each do | annotation |
comment = Comment.new(:user_id => annotation[1][:user_id], :file_id => annotation[1][:file_id], :user_type => 'InternalUser', :row => annotation[1][:row], :column => annotation[1][:column], :text => annotation[1][:text])
comment = Comment.new(:user_id => annotation[1][:user_id], :file_id => annotation[1][:file_id], :user_type => current_user.user_type, :row => annotation[1][:row], :column => annotation[1][:column], :text => annotation[1][:text])
source_file = CodeOcean::File.find(annotation[1][:file_id])
#comment = Comment.new(annotation[1].permit(:user_id, :file_id, :user_type, :row, :column, :text, :created_at, :updated_at))