@@ -432,52 +432,45 @@ module FormHelper
432
432
# <% end %>
433
433
def form_for ( record , options = { } , &block )
434
434
raise ArgumentError , "Missing block" unless block_given?
435
- html_options = options [ :html ] ||= { }
436
435
437
436
case record
438
437
when String , Symbol
438
+ model = nil
439
439
object_name = record
440
- object = nil
441
440
else
441
+ model = record
442
442
object = _object_for_form_builder ( record )
443
443
raise ArgumentError , "First argument in form cannot contain nil or be empty" unless object
444
444
object_name = options [ :as ] || model_name_from_record_or_class ( object ) . param_key
445
- apply_form_for_options! ( record , object , options )
445
+ apply_form_for_options! ( object , options )
446
446
end
447
447
448
- html_options [ :data ] = options . delete ( :data ) if options . has_key? ( :data )
449
- html_options [ :remote ] = options . delete ( :remote ) if options . has_key? ( :remote )
450
- html_options [ :method ] = options . delete ( :method ) if options . has_key? ( :method )
451
- html_options [ :enforce_utf8 ] = options . delete ( :enforce_utf8 ) if options . has_key? ( :enforce_utf8 )
452
- html_options [ :authenticity_token ] = options . delete ( :authenticity_token )
448
+ remote = options . delete ( :remote )
453
449
454
- builder = instantiate_builder ( object_name , object , options )
455
- output = capture ( builder , &block )
456
- html_options [ :multipart ] ||= builder . multipart?
450
+ if remote && !embed_authenticity_token_in_remote_forms && options [ :authenticity_token ] . blank?
451
+ options [ :authenticity_token ] = false
452
+ end
453
+
454
+ options [ :model ] = model
455
+ options [ :scope ] = object_name
456
+ options [ :local ] = !remote
457
+ options [ :skip_default_ids ] = false
458
+ options [ :allow_method_names_outside_object ] = options . fetch ( :allow_method_names_outside_object , false )
457
459
458
- html_options = html_options_for_form ( options . fetch ( :url , { } ) , html_options )
459
- form_tag_with_body ( html_options , output )
460
+ form_with ( **options , &block )
460
461
end
461
462
462
- def apply_form_for_options! ( record , object , options ) # :nodoc:
463
+ def apply_form_for_options! ( object , options ) # :nodoc:
463
464
object = convert_to_model ( object )
464
465
465
466
as = options [ :as ]
466
467
namespace = options [ :namespace ]
467
- action , method = object . respond_to? ( :persisted? ) && object . persisted? ? [ :edit , :patch ] : [ :new , :post ]
468
+ action = object . respond_to? ( :persisted? ) && object . persisted? ? :edit : :new
469
+ options [ :html ] ||= { }
468
470
options [ :html ] . reverse_merge! (
469
471
class : as ? "#{ action } _#{ as } " : dom_class ( object , action ) ,
470
472
id : ( as ? [ namespace , action , as ] : [ namespace , dom_id ( object , action ) ] ) . compact . join ( "_" ) . presence ,
471
- method : method
472
473
)
473
-
474
- if options [ :url ] != false
475
- options [ :url ] ||= if options . key? ( :format )
476
- polymorphic_path ( record , format : options . delete ( :format ) )
477
- else
478
- polymorphic_path ( record , { } )
479
- end
480
- end
481
474
end
482
475
private :apply_form_for_options!
483
476
@@ -1023,8 +1016,9 @@ def form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block)
1023
1016
# hidden field is not needed and you can pass <tt>include_id: false</tt>
1024
1017
# to prevent fields_for from rendering it automatically.
1025
1018
def fields_for ( record_name , record_object = nil , options = { } , &block )
1026
- builder = instantiate_builder ( record_name , record_object , options )
1027
- capture ( builder , &block )
1019
+ options = { model : record_object , allow_method_names_outside_object : false , skip_default_ids : false } . merge! ( options )
1020
+
1021
+ fields ( record_name , **options , &block )
1028
1022
end
1029
1023
1030
1024
# Scopes input fields with either an explicit scope or model.
@@ -1073,8 +1067,7 @@ def fields_for(record_name, record_object = nil, options = {}, &block)
1073
1067
# to work with an object as a base, like
1074
1068
# FormOptionsHelper#collection_select and DateHelper#datetime_select.
1075
1069
def fields ( scope = nil , model : nil , **options , &block )
1076
- options [ :allow_method_names_outside_object ] = true
1077
- options [ :skip_default_ids ] = !form_with_generates_ids
1070
+ options = { allow_method_names_outside_object : true , skip_default_ids : !form_with_generates_ids } . merge! ( options )
1078
1071
1079
1072
if model
1080
1073
model = _object_for_form_builder ( model )
@@ -1575,9 +1568,15 @@ def _object_for_form_builder(object) # :nodoc:
1575
1568
def html_options_for_form_with ( url_for_options = nil , model = nil , html : { } , local : !form_with_generates_remote_forms ,
1576
1569
skip_enforcing_utf8 : nil , **options )
1577
1570
html_options = options . slice ( :id , :class , :multipart , :method , :data , :authenticity_token ) . merge! ( html )
1578
- html_options [ :remote ] = !local
1571
+ html_options [ :remote ] = html . delete ( :remote ) || !local
1579
1572
html_options [ :method ] ||= :patch if model . respond_to? ( :persisted? ) && model . persisted?
1580
- html_options [ :enforce_utf8 ] = !skip_enforcing_utf8 unless skip_enforcing_utf8 . nil?
1573
+ if skip_enforcing_utf8 . nil?
1574
+ if options . key? ( :enforce_utf8 )
1575
+ html_options [ :enforce_utf8 ] = options [ :enforce_utf8 ]
1576
+ end
1577
+ else
1578
+ html_options [ :enforce_utf8 ] = !skip_enforcing_utf8
1579
+ end
1581
1580
html_options_for_form ( url_for_options . nil? ? { } : url_for_options , html_options )
1582
1581
end
1583
1582
0 commit comments