aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2016-12-18 20:17:52 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2016-12-18 20:18:48 +0100
commitb2672c739b9fe3a5b37249d899bdec9c3b9726cf (patch)
tree73fb490916bde8e4dcd74d04faecdb7959911436 /actionview
parenta52413ac91bad22de1a25660848f862a0016ba74 (diff)
downloadrails-b2672c739b9fe3a5b37249d899bdec9c3b9726cf.tar.gz
rails-b2672c739b9fe3a5b37249d899bdec9c3b9726cf.tar.bz2
rails-b2672c739b9fe3a5b37249d899bdec9c3b9726cf.zip
fields: support attributes not on model.
Ensure the support works like form_with.
Diffstat (limited to 'actionview')
-rw-r--r--actionview/lib/action_view/helpers/form_helper.rb2
-rw-r--r--actionview/test/template/form_helper/form_with_test.rb35
2 files changed, 37 insertions, 0 deletions
diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb
index ac4b4196cd..a66a56d441 100644
--- a/actionview/lib/action_view/helpers/form_helper.rb
+++ b/actionview/lib/action_view/helpers/form_helper.rb
@@ -1037,6 +1037,7 @@ module ActionView
# to work with an object as a base, like
# FormOptionHelper#collection_select and DateHelper#datetime_select.
def fields(scope = nil, model: nil, **options, &block)
+ options[:allow_method_names_outside_object] = true
options[:skip_default_ids] = true
if model
@@ -1945,6 +1946,7 @@ module ActionView
# See the docs for the <tt>ActionView::FormHelper.fields</tt> helper method.
def fields(scope = nil, model: nil, **options, &block)
+ options[:allow_method_names_outside_object] = true
options[:skip_default_ids] = true
convert_to_legacy_options(options)
diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb
index 96b797992f..dd0883e071 100644
--- a/actionview/test/template/form_helper/form_with_test.rb
+++ b/actionview/test/template/form_helper/form_with_test.rb
@@ -935,6 +935,41 @@ class FormWithActsLikeFormForTest < FormWithTest
end
end
+ def test_fields_with_attributes_not_on_model
+ form_with(model: @post) do |f|
+ concat f.fields(:comment) { |c|
+ concat c.text_field :dont_exist_on_model
+ }
+ end
+
+ expected = whole_form("/posts/123", method: :patch) do
+ '<input type="text" name="post[comment][dont_exist_on_model]">'
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_fields_with_attributes_not_on_model_deep_nested
+ @comment.save
+ form_with(scope: :posts) do |f|
+ f.fields("post[]", model: @post) do |f2|
+ f2.text_field(:id)
+ @post.comments.each do |comment|
+ concat f2.fields("comment[]", model: comment) { |c|
+ concat c.text_field(:dont_exist_on_model)
+ }
+ end
+ end
+ end
+
+ expected = whole_form do
+ '<input name="posts[post][0][comment][1][dont_exist_on_model]" type="text">'
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
+
def test_nested_fields
@comment.body = "Hello World"
form_with(model: @post) do |f|