aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb16
-rw-r--r--actionpack/test/template/form_helper_test.rb22
2 files changed, 36 insertions, 2 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 4c050cae00..2bfb896633 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -639,8 +639,20 @@ module ActionView
class_eval src, __FILE__, __LINE__
end
- def fields_for(name, *args, &block)
- name = "#{object_name}[#{name}]"
+ def fields_for(record_or_name_or_array, *args, &block)
+ case record_or_name_or_array
+ when String, Symbol
+ name = "#{object_name}[#{record_or_name_or_array}]"
+ when Array
+ object = record_or_name_or_array.last
+ name = "#{object_name}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
+ args.unshift(object)
+ else
+ object = record_or_name_or_array
+ name = "#{object_name}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
+ args.unshift(object)
+ end
+
@template.fields_for(name, *args, &block)
end
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index 136e6f1e83..37e3538b1e 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -489,6 +489,28 @@ class FormHelperTest < Test::Unit::TestCase
assert_dom_equal expected, _erbout
end
+ def test_form_for_and_fields_for_with_object
+ _erbout = ''
+
+ form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
+ _erbout.concat post_form.text_field(:title)
+ _erbout.concat post_form.text_area(:body)
+
+ post_form.fields_for(@comment) do |comment_fields|
+ _erbout.concat comment_fields.text_field(:name)
+ end
+ end
+
+ expected =
+ "<form action='http://www.example.com' id='create-post' method='post'>" +
+ "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
+ "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
+ "<input name='post[comment][name]' type='text' id='post_comment_name' value='new comment' size='30' />" +
+ "</form>"
+
+ assert_dom_equal expected, _erbout
+ end
+
class LabelledFormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(hidden_field)).each do |selector|
src = <<-END_SRC