aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/form_helper.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-02-19 21:20:15 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-02-19 21:20:15 +0100
commitd8f1ee4b41352b870f617b01099b2877f754d32c (patch)
treedd6bdf1d1ded6aab7bb926109a24e942fc740b73 /actionpack/lib/action_view/helpers/form_helper.rb
parent8ba1fc18e13c03966d411947180022c1730e81ff (diff)
parent7c0e008973e594ebf53607362c1dfbe34b693600 (diff)
downloadrails-d8f1ee4b41352b870f617b01099b2877f754d32c.tar.gz
rails-d8f1ee4b41352b870f617b01099b2877f754d32c.tar.bz2
rails-d8f1ee4b41352b870f617b01099b2877f754d32c.zip
Merge commit 'mainstream/master'
Diffstat (limited to 'actionpack/lib/action_view/helpers/form_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb30
1 files changed, 21 insertions, 9 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 37400bdbf7..4fef2b443e 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -755,7 +755,9 @@ module ActionView
end
options["checked"] = "checked" if checked
add_default_name_and_id(options)
- tag("input", options) << tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
+ hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
+ checkbox = tag("input", options)
+ hidden + checkbox
end
def to_boolean_select_tag(options = {})
@@ -964,24 +966,34 @@ module ActionView
def fields_for_with_nested_attributes(association_name, args, block)
name = "#{object_name}[#{association_name}_attributes]"
association = @object.send(association_name)
+ explicit_object = args.first if args.first.respond_to?(:new_record?)
if association.is_a?(Array)
- children = args.first.respond_to?(:new_record?) ? [args.first] : association
+ children = explicit_object ? [explicit_object] : association
+ explicit_child_index = args.last[:child_index] if args.last.is_a?(Hash)
children.map do |child|
- child_name = "#{name}[#{ child.new_record? ? new_child_id : child.id }]"
- @template.fields_for(child_name, child, *args, &block)
+ fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index}]", child, args, block)
end.join
else
- object = args.first.respond_to?(:new_record?) ? args.first : association
+ fields_for_nested_model(name, explicit_object || association, args, block)
+ end
+ end
+
+ def fields_for_nested_model(name, object, args, block)
+ if object.new_record?
@template.fields_for(name, object, *args, &block)
+ else
+ @template.fields_for(name, object, *args) do |builder|
+ @template.concat builder.hidden_field(:id)
+ block.call(builder)
+ end
end
end
- def new_child_id
- value = (@child_counter ||= 1)
- @child_counter += 1
- "new_#{value}"
+ def nested_child_index
+ @nested_child_index ||= -1
+ @nested_child_index += 1
end
end
end