From 705f7508aacbd984e5791b0347af5499008340ae Mon Sep 17 00:00:00 2001 From: Ken Johnson Date: Thu, 11 Dec 2025 12:35:19 +0000 Subject: [PATCH] Fix flash messages not appearing after file upload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changed flash message handling to ensure success and error messages are visible to users after file upload attempts. Changes: - Use flash.now for validation errors (no file, wrong type, too large) so messages display immediately without redirect - Re-render index page on validation errors instead of redirecting - Keep regular flash for success messages to persist through redirect - Refactored file listing into load_uploaded_files helper method Before: Flash messages were set but not displaying after redirect After: Users see clear feedback for all upload outcomes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app/controllers/benefit_forms_controller.rb | 41 +++++++++++++-------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/app/controllers/benefit_forms_controller.rb b/app/controllers/benefit_forms_controller.rb index 2017ddc..1524cfc 100644 --- a/app/controllers/benefit_forms_controller.rb +++ b/app/controllers/benefit_forms_controller.rb @@ -3,15 +3,7 @@ class BenefitFormsController < ApplicationController def index @benefits = Benefits.new - # List uploaded files - data_path = Rails.root.join("public", "data") - @uploaded_files = Dir.glob("#{data_path}/*").reject { |f| File.directory?(f) || File.basename(f) == '.gitkeep' }.map do |file| - { - name: File.basename(file), - size: File.size(file), - modified: File.mtime(file) - } - end.sort_by { |f| f[:modified] }.reverse + load_uploaded_files end def download @@ -28,8 +20,10 @@ class BenefitFormsController < ApplicationController file = params[:benefits][:upload] if file.nil? - flash[:error] = "Please select a file to upload" - redirect_to user_benefit_forms_path(user_id: current_user.id) + flash.now[:error] = "Please select a file to upload" + @benefits = Benefits.new + load_uploaded_files + render :index return end @@ -38,16 +32,20 @@ class BenefitFormsController < ApplicationController file_extension = File.extname(file.original_filename).downcase unless allowed_extensions.include?(file_extension) - flash[:error] = "Invalid file type. Accepted formats: PDF, DOC, DOCX, JPG, PNG. You uploaded: #{file_extension}" - redirect_to user_benefit_forms_path(user_id: current_user.id) + flash.now[:error] = "Invalid file type. Accepted formats: PDF, DOC, DOCX, JPG, PNG. You uploaded: #{file_extension}" + @benefits = Benefits.new + load_uploaded_files + render :index return end # Validate file size (10MB max) max_size = 10.megabytes if file.size > max_size - flash[:error] = "File too large. Maximum size: 10MB. Your file: #{(file.size / 1.megabyte.to_f).round(2)}MB" - redirect_to user_benefit_forms_path(user_id: current_user.id) + flash.now[:error] = "File too large. Maximum size: 10MB. Your file: #{(file.size / 1.megabyte.to_f).round(2)}MB" + @benefits = Benefits.new + load_uploaded_files + render :index return end @@ -61,4 +59,17 @@ class BenefitFormsController < ApplicationController redirect_to user_benefit_forms_path(user_id: current_user.id) end + private + + def load_uploaded_files + data_path = Rails.root.join("public", "data") + @uploaded_files = Dir.glob("#{data_path}/*").reject { |f| File.directory?(f) || File.basename(f) == '.gitkeep' }.map do |file| + { + name: File.basename(file), + size: File.size(file), + modified: File.mtime(file) + } + end.sort_by { |f| f[:modified] }.reverse + end + end