diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2010-09-26 00:28:00 +0800 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-09-26 02:13:47 +0800 |
commit | 00e7633555451eb617d68da6e53e4839dcc759d0 (patch) | |
tree | fb4bd82b6aae46fd7dcbb8e860e432caf04ba6b6 /actionpack/lib/action_view/helpers | |
parent | 63458994896c6e1b57f5c8716c637d74f1bb1c9e (diff) | |
download | rails-00e7633555451eb617d68da6e53e4839dcc759d0.tar.gz rails-00e7633555451eb617d68da6e53e4839dcc759d0.tar.bz2 rails-00e7633555451eb617d68da6e53e4839dcc759d0.zip |
More cleanup on form helpers
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 2b1d57c2b0..8abf0bfa59 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -304,7 +304,7 @@ module ActionView object_name = record object = nil else - object = record.is_a?(Array) ? record.last : record + object = record.is_a?(Array) ? record.last : record object_name = options[:as] || ActiveModel::Naming.param_key(object) apply_form_for_options!(record, options) end @@ -1171,17 +1171,14 @@ module ActionView if nested_attributes_association?(record_or_name_or_array) return fields_for_with_nested_attributes(record_or_name_or_array, args, block) else - name = "#{object_name}#{index}[#{record_or_name_or_array}]" + name = record_or_name_or_array end - when Array - object = record_or_name_or_array.last - name = "#{object_name}#{index}[#{ActiveModel::Naming.param_key(object)}]" - args.unshift(object) else - object = record_or_name_or_array - name = "#{object_name}#{index}[#{ActiveModel::Naming.param_key(object)}]" + object = record_or_name_or_array.is_a?(Array) ? record_or_name_or_array.last : record_or_name_or_array + name = ActiveModel::Naming.param_key(object) args.unshift(object) end + name = "#{object_name}#{index}[#{name}]" @template.fields_for(name, *args, &block) end @@ -1250,7 +1247,7 @@ module ActionView end def submit_default_value - object = @object.respond_to?(:to_model) ? @object.to_model : @object + object = convert_to_model(@object) key = object ? (object.persisted? ? :update : :create) : :submit model = if object.class.respond_to?(:model_name) @@ -1275,7 +1272,7 @@ module ActionView name = "#{object_name}[#{association_name}_attributes]" options = args.extract_options! association = args.shift - association = association.to_model if association.respond_to?(:to_model) + association = convert_to_model(association) if association.respond_to?(:persisted?) association = [association] if @object.send(association_name).is_a?(Array) @@ -1296,7 +1293,7 @@ module ActionView end def fields_for_nested_model(name, object, options, block) - object = object.to_model if object.respond_to?(:to_model) + object = convert_to_model(object) if object.persisted? @template.fields_for(name, object, options) do |builder| @@ -1312,6 +1309,10 @@ module ActionView @nested_child_index[name] ||= -1 @nested_child_index[name] += 1 end + + def convert_to_model(object) + object.respond_to?(:to_model) ? object.to_model : object + end end end |