# HG changeset patch # User jittat # Date 2008-04-12 09:03:12 # Node ID 9bdacfc7b6d76e2b09e1a275785d2b8d8dfb5a8b # Parent f0e9de51d9aabd117c02ba952dc3262b9b1e6357 [web] fixed test_request error when user does not put in any file git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@170 6386c4cd-e34a-4fa8-8920-d93eb39b512e diff --git a/app/controllers/test_controller.rb b/app/controllers/test_controller.rb --- a/app/controllers/test_controller.rb +++ b/app/controllers/test_controller.rb @@ -11,25 +11,30 @@ :redirect_to => { :action => :index } def index - @user = User.find(session[:user_id]) prepare_index_information - @test_requests = @user.test_requests end def submit @user = User.find(session[:user_id]) - if @user.site!=nil and @user.site.finished? - flash[:notice] = 'Error saving your test submission: Contest is over.' - redirect_to :action => 'index' and return + @submitted_test_request = TestRequest.new_from_form_params(@user,params[:test_request]) + + if @submitted_test_request.errors.length != 0 + prepare_index_information + render :action => 'index' and return end - test_request = TestRequest.new_from_form_params(@user,params[:test_request]) - if test_request.save + if @user.site!=nil and @user.site.finished? + @submitted_test_request.errors.add_to_base('Contest is over.') + prepare_index_information + render :action => 'index' and return + end + + if @submitted_test_request.save redirect_to :action => 'index' else - flash[:notice] = 'Error saving your test submission' - redirect_to :action => 'index' + prepare_index_information + render :action => 'index' end end @@ -75,8 +80,10 @@ protected def prepare_index_information + @user = User.find(session[:user_id]) @submissions = Submission.find_last_for_all_available_problems(@user.id) @problems = @submissions.collect { |submission| submission.problem } + @test_requests = @user.test_requests end end diff --git a/app/models/test_request.rb b/app/models/test_request.rb --- a/app/models/test_request.rb +++ b/app/models/test_request.rb @@ -57,6 +57,9 @@ problem.id, params[:submission_number]) test_request.input_file_name = save_input_file(params[:input_file], user, problem) + if test_request.input_file_name == nil + test_request.errors.add_to_base("No input submitted.") + end test_request.submitted_at = Time.new test_request.status_inqueue test_request @@ -84,10 +87,14 @@ new_file_name = random_input_file_name(user,problem) dirname = File.dirname(new_file_name) FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname) + + # when the user did not submit any file + return nil if tempfile=="" + if tempfile.instance_of?(Tempfile) tempfile.close FileUtils.move(tempfile.path,new_file_name) - else + else File.open(new_file_name, "wb") do |f| f.write(tempfile.read) end diff --git a/app/views/test/index.html.erb b/app/views/test/index.html.erb --- a/app/views/test/index.html.erb +++ b/app/views/test/index.html.erb @@ -30,6 +30,8 @@ } +
+<%= error_messages_for 'submitted_test_request' %> <% form_for :test_request, nil, :url => { :action => 'submit'}, :html => { :multipart => true } do |f| %> @@ -61,6 +63,7 @@ <% end %> +

Previous requests