@@ -19,7 +19,7 @@ def create
1919 uploaded_file = params [ :csv_file ]
2020
2121 if uploaded_file . blank?
22- flash [ :alert ] = "Please select a CSV file to upload"
22+ flash . now [ :alert ] = "Please select a CSV file to upload"
2323 @batch_upload = CertificationBatchUpload . new
2424 render :new , status : :unprocessable_content
2525 return
@@ -31,12 +31,15 @@ def create
3131 )
3232 @batch_upload . file . attach ( uploaded_file )
3333
34- if @batch_upload . save
35- flash [ :notice ] = "File uploaded successfully. You can now process it from the queue."
36- redirect_to certification_batch_uploads_path
37- else
38- flash [ :alert ] = "Failed to upload file: #{ @batch_upload . errors . full_messages . join ( ', ' ) } "
39- render :new , status : :unprocessable_content
34+ respond_to do |format |
35+ if @batch_upload . save
36+ format . html { redirect_to certification_batch_uploads_path , notice : "File uploaded successfully. You can now process it from the queue." }
37+ format . json { render :show , status : :created , location : @batch_upload }
38+ else
39+ message = "Failed to upload file: #{ @batch_upload . errors . full_messages . join ( ', ' ) } "
40+ format . html { redirect_to new_certification_batch_upload_path , alert : message }
41+ format . json { render json : { error : message } , status : :unprocessable_entity }
42+ end
4043 end
4144 end
4245
@@ -59,16 +62,19 @@ def results
5962
6063 # POST /staff/certification_batch_uploads/:id/process_batch
6164 def process_batch
62- unless @batch_upload . processable?
63- flash [ :alert ] = "This batch cannot be processed. Current status: #{ @batch_upload . status } "
64- redirect_to certification_batch_upload_path ( @batch_upload )
65- return
65+ respond_to do |format |
66+ if @batch_upload . processable? == false
67+ message = "This batch cannot be processed. Current status: #{ @batch_upload . status } ."
68+ format . html { redirect_to certification_batch_upload_path ( @batch_upload ) , alert : message }
69+ format . json { render json : { error : message } , status : :unprocessable_entity }
70+ elsif ProcessCertificationBatchUploadJob . perform_later ( @batch_upload . id )
71+ format . html { redirect_to certification_batch_uploads_path , notice : "Processing started for #{ @batch_upload . filename } . Results will be available shortly." }
72+ format . json { head :accepted }
73+ else
74+ format . html { redirect_to certification_batch_upload_path ( @batch_upload ) , alert : "Failed to start processing job." }
75+ format . json { render json : { error : "Failed to start processing job." } , status : :internal_server_error }
76+ end
6677 end
67-
68- ProcessCertificationBatchUploadJob . perform_later ( @batch_upload . id )
69-
70- flash [ :notice ] = "Processing started for #{ @batch_upload . filename } . Results will be available shortly."
71- redirect_to certification_batch_uploads_path
7278 end
7379
7480 private
0 commit comments