提交 e18f045b 编写于 作者: A Adam Niedzielski

form_for - fix :namespace and :as options clash

:as option should not overwrite :namespace option when
generating html id attribute of the form element. id should be prefixed
by specified namespace even if :as option is present

Add test case showing the issue and code fixing it
上级 638adfc4
* 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. * Only cache template digests if `config.cache_template_loading` id true.
*Josh Lauer*, *Justin Ridgewell* *Josh Lauer*, *Justin Ridgewell*
......
...@@ -442,10 +442,11 @@ def apply_form_for_options!(record, object, options) #:nodoc: ...@@ -442,10 +442,11 @@ def apply_form_for_options!(record, object, options) #:nodoc:
object = convert_to_model(object) object = convert_to_model(object)
as = options[:as] as = options[:as]
namespace = options[:namespace]
action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post] action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post]
options[:html].reverse_merge!( options[:html].reverse_merge!(
class: as ? "#{action}_#{as}" : dom_class(object, action), 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 method: method
) )
......
...@@ -1681,6 +1681,18 @@ def test_form_for_with_namespace_with_label ...@@ -1681,6 +1681,18 @@ def test_form_for_with_namespace_with_label
assert_dom_equal expected, output_buffer assert_dom_equal expected, output_buffer
end 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
"<input id='namespace_custom_name_title' name='custom_name[title]' type='text' value='Hello World' />"
end
assert_dom_equal expected, output_buffer
end
def test_two_form_for_with_namespace def test_two_form_for_with_namespace
form_for(@post, namespace: 'namespace_1') do |f| form_for(@post, namespace: 'namespace_1') do |f|
concat f.label(:title) concat f.label(:title)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册