Changeset 6959
- Timestamp:
- 06/06/07 16:52:37 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_view/helpers/form_helper.rb
r6951 r6959 156 156 # 157 157 # If you don't need to attach a form to a model instance, then check out FormTagHelper#form_tag. 158 def form_for(record_or_name , *args, &proc)158 def form_for(record_or_name_or_array, *args, &proc) 159 159 raise ArgumentError, "Missing block" unless block_given? 160 160 161 161 options = args.last.is_a?(Hash) ? args.pop : {} 162 162 163 case record_or_name 163 case record_or_name_or_array 164 164 when String, Symbol 165 object_name = record_or_name 165 object_name = record_or_name_or_array 166 166 when Array 167 object = record_or_name .last167 object = record_or_name_or_array.last 168 168 object_name = ActionController::RecordIdentifier.singular_class_name(object) 169 apply_form_for_options!(object, options, *record_or_name) 169 # apply_form_for_options!(object, options, *record_or_name_or_array) 170 apply_form_for_options!(record_or_name_or_array, options) 170 171 args.unshift object 171 172 else 172 object = record_or_name 173 object = record_or_name_or_array 173 174 object_name = ActionController::RecordIdentifier.singular_class_name(object) 174 apply_form_for_options!( object, options)175 apply_form_for_options!([ object ], options) 175 176 args.unshift object 176 177 end … … 181 182 end 182 183 183 def apply_form_for_options!(object, options, *nested_objects) #:nodoc: 184 def apply_form_for_options!(object_or_array, options) #:nodoc: 185 object = object_or_array.is_a?(Array) ? object_or_array.last : object_or_array 186 184 187 html_options = 185 188 if object.respond_to?(:new_record?) && object.new_record? … … 191 194 options[:html] ||= {} 192 195 options[:html].reverse_merge!(html_options) 193 options[:url] ||= polymorphic_path(object , *nested_objects)196 options[:url] ||= polymorphic_path(object_or_array) 194 197 end 195 198 trunk/actionpack/test/template/form_helper_test.rb
r6951 r6959 642 642 end 643 643 644 def polymorphic_path(object, *nested_objects) 645 if nested_objects.empty? 646 if object.new_record? 644 def polymorphic_path(record_or_hash_or_array) 645 if record_or_hash_or_array.is_a?(Array) 646 record = record_or_hash_or_array.pop 647 array = record_or_hash_or_array 648 else 649 record = record_or_hash_or_array 650 array = [ ] 651 end 652 653 if array.size > 0 654 if record.new_record? 655 "/posts/123/comments" 656 else 657 "/posts/123/comments/#{record.id}" 658 end 659 else 660 if record.new_record? 647 661 "/posts" 648 662 else 649 "/posts/#{object.id}" 650 end 651 else 652 if object.new_record? 653 "/posts/123/comments" 654 else 655 "/posts/123/comments/#{object.id}" 656 end 657 end 663 "/posts/#{record.id}" 664 end 665 end 658 666 end 659 667 end