Deal with nested fields_for too [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8253 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2007-12-02 01:10:50 +00:00
parent d0ce7cd4c7
commit 84ca7e6ef7
2 changed files with 36 additions and 2 deletions

View File

@@ -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

View File

@@ -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