mirror of
https://github.com/github/rails.git
synced 2026-01-30 08:48:06 -05:00
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:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user