From c17a21a4cfcb9ca2ad2efc3579eb904cbf93dc11 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Tue, 1 Aug 2017 19:24:38 +0200 Subject: [PATCH] Add a randomized name attribute to all form fields This is a workaround for a Bug in Firefox described at https://bugzilla.mozilla.org/show_bug.cgi?id=1279253 --- app/helpers/application_helper.rb | 7 +++++++ test/helpers/application_helper_test.rb | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cb0c27715..ae41eae8c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1180,6 +1180,13 @@ module ApplicationHelper fields_for(*args, &proc) 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.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb index 6a461c31a..b9a0260ab 100644 --- a/test/helpers/application_helper_test.rb +++ b/test/helpers/application_helper_test.rb @@ -1579,4 +1579,13 @@ RAW assert_equal '0:45', html_hours('0:45') assert_equal '0.75', html_hours('0.75') end + + def test_form_for_includes_name_attribute + assert_match(/name="new_issue-[a-z0-9]{8}"/, form_for(Issue.new){}) + end + + def test_labelled_form_for_includes_name_attribute + assert_match(/name="new_issue-[a-z0-9]{8}"/, labelled_form_for(Issue.new){}) + end + end -- 2.13.0