From 88e78e8b6f1117309441faf898abdf1711b8c30b Mon Sep 17 00:00:00 2001 From: Joost Baaij Date: Mon, 20 Sep 2010 15:38:47 +0200 Subject: [PATCH] file_field_tag makes the enclosing form multipart (see b17b980). This is a first implementation using String#include? to check the captured block. Consider this ugly, but a good working implementation. A better approach would probably be an inner class to build the form, but that's quite an undertaking. --- .../lib/action_view/helpers/form_tag_helper.rb | 5 +++-- actionpack/test/template/form_tag_helper_test.rb | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 43ffadc..2caf90a 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -564,9 +564,10 @@ module ActionView end def form_tag_in_block(html_options, &block) - content = capture(&block) + content = capture(&block) || "" output = ActiveSupport::SafeBuffer.new - output.safe_concat(form_tag_html(html_options)) + default_options = content.include?("type=\"file\"") ? {:enctype => "multipart/form-data"} : {} + output.safe_concat(form_tag_html(default_options.merge(html_options))) output << content output.safe_concat("") end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 532f086..4af8dce 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -71,6 +71,12 @@ class FormTagHelperTest < ActionView::TestCase assert_dom_equal expected, actual end + def test_form_tag_with_file_field_generate_multipart + actual = form_tag() { file_field_tag 'image' } + expected = whole_form("http://www.example.com", :enctype => true) { file_field_tag 'image' } + assert_equal expected, actual + end + def test_form_tag_with_method_put actual = form_tag({}, { :method => :put }) expected = whole_form("http://www.example.com", :method => :put) -- 1.7.0.3