diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 6240dc6ac656ea3d6299aa0d8ffec5682f545314..edc1717459e324fd62bb753ce23f02d559ca4322 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,10 @@ +* Fix `form_for` when both `namespace` and `as` options are present + + `as` option no longer overwrites `namespace` option when generating + html id attribute of the form element + + *Adam Niedzielski* + * Only cache template digests if `config.cache_template_loading` id true. *Josh Lauer*, *Justin Ridgewell* diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index 8a4830d887e813aa8402f54dec979f5558d472ef..ead7871fc52de46191122a02c718d5fa6ff5462b 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -442,10 +442,11 @@ def apply_form_for_options!(record, object, options) #:nodoc: object = convert_to_model(object) as = options[:as] + namespace = options[:namespace] action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post] options[:html].reverse_merge!( class: as ? "#{action}_#{as}" : dom_class(object, action), - id: as ? "#{action}_#{as}" : [options[:namespace], dom_id(object, action)].compact.join("_").presence, + id: (as ? [namespace, action, as] : [namespace, dom_id(object, action)]).compact.join("_").presence, method: method ) diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index 8cca43d7ca947d088cd27756573fc5c37161c6b1..944884c9dd5e7fe3e4abbae9e80fd6278831c6c7 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -1681,6 +1681,18 @@ def test_form_for_with_namespace_with_label assert_dom_equal expected, output_buffer end + def test_form_for_with_namespace_and_as_option + form_for(@post, namespace: 'namespace', as: 'custom_name') do |f| + concat f.text_field(:title) + end + + expected = whole_form('/posts/123', 'namespace_edit_custom_name', 'edit_custom_name', method: 'patch') do + "" + end + + assert_dom_equal expected, output_buffer + end + def test_two_form_for_with_namespace form_for(@post, namespace: 'namespace_1') do |f| concat f.label(:title)