1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

file_field propagates up multipart property even inside of fields_for

This commit is contained in:
Santiago Pastorino 2010-09-20 00:10:16 -07:00
parent 800bab79b3
commit 0523b55ab9
2 changed files with 34 additions and 8 deletions

View file

@ -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

View file

@ -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)