diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2010-09-20 00:10:16 -0700 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2010-09-20 01:11:55 -0700 |
commit | 0523b55ab9f823b0cb3643714e890b2c2077820e (patch) | |
tree | 190d49dec912f0b34c60e0483c17654ba282b579 | |
parent | 800bab79b38949d454e0ef9b33f7cc1be912e0fa (diff) | |
download | rails-0523b55ab9f823b0cb3643714e890b2c2077820e.tar.gz rails-0523b55ab9f823b0cb3643714e890b2c2077820e.tar.bz2 rails-0523b55ab9f823b0cb3643714e890b2c2077820e.zip |
file_field propagates up multipart property even inside of fields_for
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 21 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 21 |
2 files changed, 34 insertions, 8 deletions
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index ea59d328ea..6ca956f879 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -317,8 +317,8 @@ module ActionView options[:html] ||= {} options[:html][:remote] = options.delete(:remote) - builder = instantiate_builder(object_name, object, options, &proc) - fields_for = capture(builder, &proc) + builder = options[:parent_builder] = instantiate_builder(object_name, object, options, &proc) + fields_for = fields_for(object_name, object, options, &proc) default_options = builder.multipart? ? { :multipart => true } : {} output = form_tag(options.delete(:url) || {}, default_options.merge!(options.delete(:html) || {})) output << fields_for @@ -1119,9 +1119,14 @@ module ActionView attr_accessor :object_name, :object, :options - attr_reader :multipart + attr_reader :multipart, :parent_builder alias :multipart? :multipart + def multipart=(multipart) + @multipart = multipart + parent_builder.multipart = multipart if parent_builder + end + def self.model_name @model_name ||= Struct.new(:partial_path).new(name.demodulize.underscore.sub!(/_builder$/, '')) end @@ -1133,6 +1138,7 @@ module ActionView def initialize(object_name, object, template, options, proc) @nested_child_index = {} @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc + @parent_builder = options[:parent_builder] @default_options = @options ? @options.slice(:index) : {} if @object_name.to_s.match(/\[\]$/) if object ||= @template.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:to_param) @@ -1166,10 +1172,9 @@ module ActionView index = "" end - if options[:builder] - args << {} unless args.last.is_a?(Hash) - args.last[:builder] ||= options[:builder] - end + args << {} unless args.last.is_a?(Hash) + args.last[:builder] ||= options[:builder] + args.last[:parent_builder] = self case record_or_name_or_array when String, Symbol @@ -1209,7 +1214,7 @@ module ActionView end def file_field(method, options = {}) - @multipart = true + self.multipart = true @template.file_field(@object_name, method, objectify_options(options)) end # Add the submit button for the given form. When no value is given, it checks diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 2f20bfa719..d40dd409b8 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -689,6 +689,27 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_fields_for_with_file_field_generate_multipart + Comment.send :attr_accessor, :file + + assert_deprecated do + form_for(:post, @post) do |f| + concat f.fields_for(:comment, @post) { |c| + concat c.file_field(:file) + } + end + end + + expected = + "<form accept-charset='UTF-8' action='/' method='post' enctype='multipart/form-data'>" + + snowman + + "<input name='post[comment][file]' type='file' id='post_comment_file' />" + + "</form>" + + assert_dom_equal expected, output_buffer + end + + def test_form_for_with_format form_for(@post, :format => :json, :html => { :id => "edit_post_123", :class => "edit_post" }) do |f| concat f.label(:title) |