Skip to content

Commit

Permalink
Test for extractable_options? within nested fields_for.
Browse files Browse the repository at this point in the history
This fixes an error when a record object that is a subclass of Hash is
passed to fields_for, which is incorrectly interpreted as field options.
  • Loading branch information
spohlenz committed Jun 20, 2011
1 parent 2df2bfd commit e69eed0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -1241,7 +1241,7 @@ def fields_for_with_index(record_name, record_object = nil, fields_options = {},
end end


def fields_for(record_name, record_object = nil, fields_options = {}, &block) def fields_for(record_name, record_object = nil, fields_options = {}, &block)
fields_options, record_object = record_object, nil if record_object.is_a?(Hash) fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
fields_options[:builder] ||= options[:builder] fields_options[:builder] ||= options[:builder]
fields_options[:parent_builder] = self fields_options[:parent_builder] = self


Expand Down
18 changes: 18 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -1689,6 +1689,24 @@ def test_nested_fields_uses_unique_indices_for_different_collection_associations
assert_dom_equal expected, output_buffer assert_dom_equal expected, output_buffer
end end


def test_nested_fields_for_with_hash_like_model
@author = Author.new
def @author.is_a?(klass); klass == Hash; end
def @author.extractable_options?; false; end

form_for(@post) do |f|
concat f.fields_for(:author, @author) { |af|
concat af.text_field(:name)
}
end

expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'put') do
'<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="new author" />'
end

assert_dom_equal expected, output_buffer
end

def test_fields_for def test_fields_for
output_buffer = fields_for(:post, @post) do |f| output_buffer = fields_for(:post, @post) do |f|
concat f.text_field(:title) concat f.text_field(:title)
Expand Down

0 comments on commit e69eed0

Please sign in to comment.