Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow form_for and fields_for to work with indexed form inputs. [Jere…

…my Kemper, Matt Lyon]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4613 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 101ebc996a241fab4adcbbaf6c8930dddde8d091 1 parent 9ac7afc
@technoweenie technoweenie authored
View
5 actionpack/CHANGELOG
@@ -1,5 +1,10 @@
*SVN*
+* Allow form_for and fields_for to work with indexed form inputs. [Jeremy Kemper, Matt Lyon]
+
+ <% form_for 'post[]', @post do |f| -%>
+ <% end -%>
+
* Remove leak in development mode by replacing define_method with module_eval. [Nicholas Seckar]
* Provide support for decimal columns to form helpers. Closes #5672. [dave@pragprog.com]
View
6 actionpack/lib/action_view/helpers/form_helper.rb
@@ -238,7 +238,11 @@ def initialize(object_name, method_name, template_object, local_binding = nil, o
@template_object, @local_binding = template_object, local_binding
@object = object
if @object_name.sub!(/\[\]$/,"")
- @auto_index = @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}").id_before_type_cast
+ if object ||= @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:id_before_type_cast)
+ @auto_index = object.id_before_type_cast
+ else
+ raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to id_before_type_cast: #{object.inspect}"
+ end
end
end
View
18 actionpack/test/template/form_helper_test.rb
@@ -279,6 +279,24 @@ def test_form_for_without_object
assert_dom_equal expected, _erbout
end
+
+ def test_form_for_with_index
+ _erbout = ''
+
+ form_for("post[]", @post) do |f|
+ _erbout.concat f.text_field(:title)
+ _erbout.concat f.text_area(:body)
+ _erbout.concat f.check_box(:secret)
+ end
+
+ 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][secret]' type='hidden' value='0' />" +
+ "</form>"
+ end
def test_fields_for
_erbout = ''

0 comments on commit 101ebc9

Please sign in to comment.
Something went wrong with that request. Please try again.