Permalink
Browse files

Allow fields_for to be nested in form_for

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5965 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 932e7b0 commit b0a1aa7e7e4bc1b0a714fd143cc91944e2f4d230 @jamis jamis committed Jan 17, 2007
@@ -1,5 +1,7 @@
*SVN*
+* Allow fields_for to be nested inside form_for, so that the name and id get properly constructed [Jamis Buck]
+
* Allow inGroupsOf and eachSlice to be called through rjs. #7046 [Cody Fauser]
* Allow exempt_from_layout :rhtml. #6742, #7026 [dcmanges, Squeegy]
@@ -401,11 +401,15 @@ def tag_name_with_index(index)
end
def tag_id
- "#{@object_name}_#{@method_name}"
+ "#{sanitized_object_name}_#{@method_name}"
end
def tag_id_with_index(index)
- "#{@object_name}_#{index}_#{@method_name}"
+ "#{sanitized_object_name}_#{index}_#{@method_name}"
+ end
+
+ def sanitized_object_name
+ @object_name.gsub(/[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
end
end
@@ -420,15 +424,20 @@ def initialize(object_name, object, template, options, proc)
@object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
end
- (field_helpers - %w(check_box radio_button)).each do |selector|
+ (field_helpers - %w(check_box radio_button fields_for)).each do |selector|
src = <<-end_src
def #{selector}(method, options = {})
@template.send(#{selector.inspect}, @object_name, method, options.merge(:object => @object))
end
end_src
class_eval src, __FILE__, __LINE__
end
-
+
+ def fields_for(name, *args, &block)
+ name = "#{object_name}[#{name}]"
+ @template.fields_for(name, *args, &block)
+ end
+
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
@template.check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value)
end
@@ -297,11 +297,28 @@ def test_form_for_with_index
expected =
"<form action='http://www.example.com' method='post'>" +
- "<input name='post[123][title]' size='30' type='text' id='post_title' value='Hello World' />" +
- "<textarea name='post[123][body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
- "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
+ "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
+ "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
+ "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
"<input name='post[123][secret]' type='hidden' value='0' />" +
"</form>"
+
+ assert_dom_equal expected, _erbout
+ end
+
+ def test_nested_fields_for
+ _erbout = ''
+ form_for(:post, @post) do |f|
+ f.fields_for(:comment, @post) do |c|
+ _erbout.concat c.text_field(:title)
+ end
+ end
+
+ expected = "<form action='http://www.example.com' method='post'>" +
+ "<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" +
+ "</form>"
+
+ assert_dom_equal expected, _erbout
end
def test_fields_for
@@ -339,6 +356,16 @@ def test_fields_for_without_object
assert_dom_equal expected, _erbout
end
+ def test_fields_for_object_with_bracketed_name
+ _erbout = ''
+ fields_for("author[post]", @post) do |f|
+ _erbout.concat f.text_field(:title)
+ end
+
+ assert_dom_equal "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />",
+ _erbout
+ end
+
def test_form_builder_does_not_have_form_for_method
assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
end

0 comments on commit b0a1aa7

Please sign in to comment.