From c0f21a2e6bf896d513fcd243e21047823afad401 Mon Sep 17 00:00:00 2001
From: Takashi Kato <tohosaku@users.osdn.me>
Date: Sat, 25 Oct 2025 14:20:17 +0900
Subject: [PATCH 2/3] Fix for https://github.com/rails/rails/pull/55404

Move FormHelper patch to initializer
---
 app/helpers/application_helper.rb |  7 -------
 config/initializers/10-patches.rb | 19 +++++++++++++++++++
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 5fc725a82d..b976e07f7f 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1570,13 +1570,6 @@ module ApplicationHelper
     fields_for(*args, &)
   end
 
-  def form_tag_html(html_options)
-    # Set a randomized name attribute on all form fields by default
-    # as a workaround to https://bugzilla.mozilla.org/show_bug.cgi?id=1279253
-    html_options['name'] ||= "#{html_options['id'] || 'form'}-#{SecureRandom.hex(4)}"
-    super
-  end
-
   # Render the error messages for the given objects
   def error_messages_for(*objects)
     objects = objects.filter_map {|o| o.is_a?(String) ? instance_variable_get(:"@#{o}") : o}
diff --git a/config/initializers/10-patches.rb b/config/initializers/10-patches.rb
index 345868410b..667ecc409c 100644
--- a/config/initializers/10-patches.rb
+++ b/config/initializers/10-patches.rb
@@ -102,6 +102,25 @@ module ActionView
       super
     end
   end)
+  module Helpers
+    # Set a randomized name attribute on all form fields by default
+    # as a workaround to https://bugzilla.mozilla.org/show_bug.cgi?id=1279253
+    FormHelper.prepend(Module.new do
+      def form_tag_html(html_options)
+        randomized_name_attribute(html_options)
+        super
+      end
+
+      def form_tag_with_body(html_options, output)
+        randomized_name_attribute(html_options)
+        super
+      end
+
+      def randomized_name_attribute(html_options)
+        html_options['name'] ||= "#{html_options['id'] || 'form'}-#{SecureRandom.hex(4)}"
+      end
+    end)
+  end
 end
 
 module ActionController
-- 
2.47.3

