Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1778 from spohlenz/hash-models

Fix nested fields_for when Hash-based model is passed.
  • Loading branch information...
commit 3c8c37984865221bdb6e1ed0a04a30d2785a3b85 2 parents 6404f8a + 237f870
@josevalim josevalim authored
View
2  actionpack/lib/action_view/helpers/form_helper.rb
@@ -1241,7 +1241,7 @@ def fields_for_with_index(record_name, record_object = nil, fields_options = {},
end
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[:parent_builder] = self
View
11 actionpack/test/lib/controller/fake_models.rb
@@ -170,6 +170,17 @@ class Author < Comment
def post_attributes=(attributes); end
end
+class HashBackedAuthor < Hash
+ extend ActiveModel::Naming
+ include ActiveModel::Conversion
+
+ def persisted?; false; end
+
+ def name
+ "hash backed author"
+ end
+end
+
module Blog
def self._railtie
self
View
16 actionpack/test/template/form_helper_test.rb
@@ -1689,6 +1689,22 @@ def test_nested_fields_uses_unique_indices_for_different_collection_associations
assert_dom_equal expected, output_buffer
end
+ def test_nested_fields_for_with_hash_like_model
+ @author = HashBackedAuthor.new
+
+ 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="hash backed author" />'
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
def test_fields_for
output_buffer = fields_for(:post, @post) do |f|
concat f.text_field(:title)
Please sign in to comment.
Something went wrong with that request. Please try again.