From f400209084fabb00e18c3325e1933f4543fce94c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 10 Feb 2009 13:10:46 -0600 Subject: Move checkbox hidden field before the actual checkbox so the actual value doesn't get clobbered [#1863 state:resolved] --- actionpack/lib/action_view/helpers/form_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/helpers/form_helper.rb') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 0651f75cfb..3925978217 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -753,7 +753,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 = {}) -- cgit v1.2.3 From 5dbc9d40a49f5f0f50c2f3ebe6dda942f0e61562 Mon Sep 17 00:00:00 2001 From: Lance Ivy Date: Sun, 8 Feb 2009 14:23:35 +0100 Subject: Changed API of NestedAttributes to take an array, or hash with index keys, of hashes that have the id on the inside of the attributes hash and updated the FormBuilder to produce such hashes. Also fixed NestedAttributes with composite ids. Signed-off-by: Michael Koziarski Signed-off-by: Eloy Duran [#1892 state:committed] --- actionpack/lib/action_view/helpers/form_helper.rb | 26 ++++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_view/helpers/form_helper.rb') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 3925978217..568687e9e0 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -964,24 +964,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 -- cgit v1.2.3