Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Do not call fields_for from form_for, to avoid instantiating two buil…

…ders
  • Loading branch information...
commit 115a37d9c1b2d8868a188a23528e3397f6761971 1 parent 94ecbb7
@carlosantoniodasilva carlosantoniodasilva authored
View
10 actionpack/lib/action_view/helpers/form_helper.rb
@@ -388,9 +388,9 @@ module FormHelper
# In many cases you will want to wrap the above in another helper, so you
# could do something like the following:
#
- # def labelled_form_for(record_or_name_or_array, *args, &proc)
+ # def labelled_form_for(record_or_name_or_array, *args, &block)
# options = args.extract_options!
- # form_for(record_or_name_or_array, *(args << options.merge(builder: LabellingFormBuilder)), &proc)
+ # form_for(record_or_name_or_array, *(args << options.merge(builder: LabellingFormBuilder)), &block)
# end
#
# If you don't need to attach a form to a model instance, then check out
@@ -412,7 +412,7 @@ module FormHelper
# <%= form_for @invoice, url: external_url, authenticity_token: false do |f|
# ...
# <% end %>
- def form_for(record, options = {}, &proc)
+ def form_for(record, options = {}, &block)
raise ArgumentError, "Missing block" unless block_given?
options[:html] ||= {}
@@ -434,9 +434,9 @@ def form_for(record, options = {}, &proc)
options[:html][:authenticity_token] = options.delete(:authenticity_token)
builder = options[:parent_builder] = instantiate_builder(object_name, object, options)
- fields_for = fields_for(object_name, object, options, &proc)
+ output = capture(builder, &block)
default_options = builder.form_tag_attributes
- form_tag(options[:url] || {}, default_options) { fields_for }
+ form_tag(options[:url] || {}, default_options) { output }
end
def apply_form_for_options!(record, object, options) #:nodoc:
View
14 actionpack/test/template/form_helper_test.rb
@@ -1171,7 +1171,6 @@ def test_fields_for_with_file_field_generate_multipart
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)
@@ -2693,6 +2692,19 @@ def initialize(object_name, object, template, options, block)
end
end
+ def test_form_for_only_instantiates_builder_once
+ initialization_count = 0
+ builder_class = Class.new(ActionView::Helpers::FormBuilder) do
+ define_method :initialize do |*args|
+ super(*args)
+ initialization_count += 1
+ end
+ end
+
+ form_for(@post, builder: builder_class) { }
+ assert_equal 1, initialization_count, 'form builder instantiated more than once'
+ end
+
protected
def hidden_fields(method = nil)

1 comment on commit 115a37d

Please sign in to comment.
Something went wrong with that request. Please try again.