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

file_field makes the enclosing form multipart

This commit is contained in:
Santiago Pastorino 2010-09-16 23:31:57 -07:00
parent e808224652
commit b17b980a15
3 changed files with 56 additions and 19 deletions

View file

@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)*
* file_field automatically adds :multipart => true to the enclosing form. [Santiago Pastorino]
* Renames csrf_meta_tag -> csrf_meta_tags, and aliases csrf_meta_tag for backwards compatibility. [fxn]
* Add Rack::Cache to the default stack. Create a Rails store that delegates to the Rails cache, so by default, whatever caching layer you are using will be used

View file

@ -317,8 +317,11 @@ module ActionView
options[:html] ||= {}
options[:html][:remote] = options.delete(:remote)
output = form_tag(options.delete(:url) || {}, options.delete(:html) || {})
output << fields_for(object_name, object, options, &proc)
builder = instantiate_builder(object_name, object, options, &proc)
fields_for = capture(builder, &proc)
default_options = builder.multipart? ? { :multipart => true } : {}
output = form_tag(options.delete(:url) || {}, default_options.merge!(options.delete(:html) || {}))
output << fields_for
output.safe_concat('</form>')
end
@ -529,22 +532,7 @@ module ActionView
# <% end %>
# <% end %>
def fields_for(record, record_object = nil, options = nil, &block)
raise ArgumentError, "Missing block" unless block_given?
options, record_object = record_object, nil if record_object.is_a?(Hash)
options ||= {}
case record
when String, Symbol
object = record_object
object_name = record
else
object = record
object_name = ActiveModel::Naming.param_key(object)
end
builder = options[:builder] || ActionView::Base.default_form_builder
capture(builder.new(object_name, object, self, options, block), &block)
capture(instantiate_builder(record, record_object, options, &block), &block)
end
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
@ -848,6 +836,27 @@ module ActionView
def range_field(object_name, method, options = {})
InstanceTag.new(object_name, method, self, options.delete(:object)).to_number_field_tag("range", options)
end
private
def instantiate_builder(record, record_object = nil, options = nil, &block)
raise ArgumentError, "Missing block" unless block_given?
options, record_object = record_object, nil if record_object.is_a?(Hash)
options ||= {}
case record
when String, Symbol
object = record_object
object_name = record
else
object = record
object_name = ActiveModel::Naming.param_key(object)
end
builder = options[:builder] || ActionView::Base.default_form_builder
builder.new(object_name, object, self, options, block)
end
end
module InstanceTagMethods #:nodoc:
@ -1112,6 +1121,9 @@ module ActionView
attr_accessor :object_name, :object, :options
attr_reader :multipart
alias :multipart? :multipart
def self.model_name
@model_name ||= Struct.new(:partial_path).new(name.demodulize.underscore.sub!(/_builder$/, ''))
end
@ -1131,9 +1143,10 @@ module ActionView
raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
end
end
@multipart = nil
end
(field_helpers - %w(label check_box radio_button fields_for hidden_field)).each do |selector|
(field_helpers - %w(label check_box radio_button fields_for hidden_field file_field)).each do |selector|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{selector}(method, options = {}) # def text_field(method, options = {})
@template.send( # @template.send(
@ -1197,6 +1210,10 @@ module ActionView
@template.hidden_field(@object_name, method, objectify_options(options))
end
def file_field(method, options = {})
@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
# if the object is a new resource or not to create the proper label:
#

View file

@ -665,6 +665,24 @@ class FormHelperTest < ActionView::TestCase
assert_dom_equal expected, output_buffer
end
def test_form_for_with_file_field_generate_multipart
Post.send :attr_accessor, :file
assert_deprecated do
form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
concat f.file_field(:file)
end
end
expected =
"<form accept-charset='UTF-8' action='/' id='create-post' method='post' enctype='multipart/form-data'>" +
snowman +
"<input name='post[file]' type='file' id='post_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)