diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-15 15:51:20 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-15 15:51:20 -0700 |
commit | 6e581cce1c1116d71484a5a4a5f92f0ea6fd29e4 (patch) | |
tree | 82b5adaf04e54bf03fd5f51963c7dccc58928729 /actionpack/lib/action_view | |
parent | dde82a44d702f8416905476bbea5842157d6a508 (diff) | |
parent | 82f33dce9c6229bee35af35a9aa3bc9c144737f3 (diff) | |
download | rails-6e581cce1c1116d71484a5a4a5f92f0ea6fd29e4.tar.gz rails-6e581cce1c1116d71484a5a4a5f92f0ea6fd29e4.tar.bz2 rails-6e581cce1c1116d71484a5a4a5f92f0ea6fd29e4.zip |
Merge pull request #568 from carlosantoniodasilva/fields_for_fixes
Fields for fixes for non nested attributes and without object
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 62 |
1 files changed, 26 insertions, 36 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index b6bb90a3ce..07e2c8d341 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -243,7 +243,7 @@ module ActionView # # === Setting the method # - # You can force the form to use the full array of HTTP verbs by setting + # You can force the form to use the full array of HTTP verbs by setting # # :method => (:get|:post|:put|:delete) # @@ -584,8 +584,8 @@ module ActionView # <% end %> # ... # <% end %> - def fields_for(record, record_object = nil, options = {}, &block) - builder = instantiate_builder(record, record_object, options, &block) + def fields_for(record_name, record_object = nil, options = {}, &block) + builder = instantiate_builder(record_name, record_object, options, &block) output = capture(builder, &block) output.concat builder.hidden_field(:id) if output && options[:hidden_field_id] && !builder.emitted_hidden_id? output @@ -898,16 +898,13 @@ module ActionView private - def instantiate_builder(record, *args, &block) - options = args.extract_options! - record_object = args.shift - - case record + def instantiate_builder(record_name, record_object, options, &block) + case record_name when String, Symbol object = record_object - object_name = record + object_name = record_name else - object = record + object = record_name object_name = ActiveModel::Naming.param_key(object) end @@ -1219,35 +1216,30 @@ module ActionView RUBY_EVAL end - def fields_for(record_or_name_or_array, *args, &block) - if options.has_key?(:index) - index = "[#{options[:index]}]" - elsif defined?(@auto_index) - self.object_name = @object_name.to_s.sub(/\[\]$/,"") - index = "[#{@auto_index}]" - else - index = "" - end - - args << {} unless args.last.is_a?(Hash) - args.last[:builder] ||= options[:builder] - args.last[:parent_builder] = self + 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[:builder] ||= options[:builder] + fields_options[:parent_builder] = self - case record_or_name_or_array + case record_name when String, Symbol - if nested_attributes_association?(record_or_name_or_array) - return fields_for_with_nested_attributes(record_or_name_or_array, args, block) - else - name = record_or_name_or_array + if nested_attributes_association?(record_name) + return fields_for_with_nested_attributes(record_name, record_object, fields_options, block) end else - 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) + record_object = record_name.is_a?(Array) ? record_name.last : record_name + record_name = ActiveModel::Naming.param_key(record_object) + end + + index = if options.has_key?(:index) + "[#{options[:index]}]" + elsif defined?(@auto_index) + self.object_name = @object_name.to_s.sub(/\[\]$/,"") + "[#{@auto_index}]" end - name = "#{object_name}#{index}[#{name}]" + record_name = "#{object_name}#{index}[#{record_name}]" - @template.fields_for(name, *args, &block) + @template.fields_for(record_name, record_object, fields_options, &block) end def label(method, text = nil, options = {}, &block) @@ -1336,10 +1328,8 @@ module ActionView @object.respond_to?("#{association_name}_attributes=") end - def fields_for_with_nested_attributes(association_name, args, block) + def fields_for_with_nested_attributes(association_name, association, options, block) name = "#{object_name}[#{association_name}_attributes]" - options = args.extract_options! - association = args.shift association = convert_to_model(association) if association.respond_to?(:persisted?) |