Permalink
Browse files

Resolve multiple nested fields_for

Closes #423
  • Loading branch information...
1 parent 1d18d47 commit 9ef127505c40d6ed6b46194e0e252800735d23e0 @bcardarella bcardarella committed Nov 22, 2012
@@ -51,7 +51,15 @@ def apply_form_for_options!(object_or_array, options)
def fields_for(record_or_name_or_array, record_object = nil, options = {}, &block)
output = super
- @validators.merge!(options[:validators]) if @validators
+ if @validators
+ options[:validators].each do |key, value|
+ if @validators.key?(key)
+ @validators[key].merge! value
+ else
+ @validators[key] = value
+ end
+ end
+ end
output
end
@@ -221,6 +221,25 @@ def test_nested_fields_for_inherit_validation_settings
assert_equal expected, output_buffer
end
+ def test_multiple_nested_fields_for_inherit_validation_settings
+ form_for(@post, :validate => true) do |f|
+ concat f.fields_for(:comment, @comment) { |c|
+ concat c.text_field(:title)
+ }
+ concat f.fields_for(:comment, @comment) { |c|
+ concat c.text_field(:body)
+ }
+ end
+
+ validators = {'post[comment][title]' => {:presence => [{:message => "can't be blank"}]}, 'post[comment][body]' => {:presence => [{:message => "can't be blank"}]}}
+ expected = whole_form('/posts', 'new_post', 'new_post', :validators => validators) do
+ %{<input id="post_comment_title" name="post[comment][title]" size="30" type="text" />} +
+ %{<input id="post_comment_body" name="post[comment][body]" size="30" type="text" />}
+ end
+
+ assert_equal expected, output_buffer
+ end
+
def test_nested_fields_for_with_nested_attributes
form_for(@post, :validate => true) do |f|
concat f.fields_for(:comments, [@comment]) { |c|
@@ -533,7 +552,7 @@ def test_added_validators_defaulting_to_all
}
end
- validators = {'post[comment][title]' => {:presence => [{:message => "can't be blank"}]}}
+ validators = {'post[comment][title]' => {:presence => [{:message => "can't be blank"}]},'post[comment][body]' => {:presence => [{:message => "can't be blank"}]}}
expected = whole_form('/posts', 'new_post', 'new_post', :validators => validators)
assert_equal expected, output_buffer
end
@@ -4,8 +4,8 @@ class Comment
include ActiveModel::Validations
include ActiveModel::Conversion
- attr_reader :id, :post_id, :title
- validates :title, :presence => true
+ attr_reader :id, :post_id, :title, :body
+ validates :title, :body, :presence => true
def initialize(params={})
params.each do |attr, value|

0 comments on commit 9ef1275

Please sign in to comment.