Skip to content

Commit

Permalink
Allow form_for and fields_for to work with indexed form inputs. [Jere…
Browse files Browse the repository at this point in the history
…my Kemper, Matt Lyon]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4613 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Jul 18, 2006
1 parent 9ac7afc commit 101ebc9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,10 @@
*SVN* *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] * 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] * Provide support for decimal columns to form helpers. Closes #5672. [dave@pragprog.com]
Expand Down
6 changes: 5 additions & 1 deletion actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -238,7 +238,11 @@ def initialize(object_name, method_name, template_object, local_binding = nil, o
@template_object, @local_binding = template_object, local_binding @template_object, @local_binding = template_object, local_binding
@object = object @object = object
if @object_name.sub!(/\[\]$/,"") 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
end end


Expand Down
18 changes: 18 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -279,6 +279,24 @@ def test_form_for_without_object


assert_dom_equal expected, _erbout assert_dom_equal expected, _erbout
end 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 def test_fields_for
_erbout = '' _erbout = ''
Expand Down

0 comments on commit 101ebc9

Please sign in to comment.