@@ -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
@@ -757,8 +750,7 @@ def apply_form_for_options!(record, object, options) # :nodoc:
757
750
# form_with(**options.merge(builder: LabellingFormBuilder), &block)
758
751
# end
759
752
def form_with ( model : nil , scope : nil , url : nil , format : nil , **options , &block )
760
- options [ :allow_method_names_outside_object ] = true
761
- options [ :skip_default_ids ] = !form_with_generates_ids
753
+ options = { allow_method_names_outside_object : true , skip_default_ids : !form_with_generates_ids } . merge! ( options )
762
754
763
755
if model
764
756
if url != false
@@ -1024,8 +1016,9 @@ def form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block)
1024
1016
# hidden field is not needed and you can pass <tt>include_id: false</tt>
1025
1017
# to prevent fields_for from rendering it automatically.
1026
1018
def fields_for ( record_name , record_object = nil , options = { } , &block )
1027
- builder = instantiate_builder ( record_name , record_object , options )
1028
- 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 )
1029
1022
end
1030
1023
1031
1024
# Scopes input fields with either an explicit scope or model.
@@ -1074,8 +1067,7 @@ def fields_for(record_name, record_object = nil, options = {}, &block)
1074
1067
# to work with an object as a base, like
1075
1068
# FormOptionsHelper#collection_select and DateHelper#datetime_select.
1076
1069
def fields ( scope = nil , model : nil , **options , &block )
1077
- options [ :allow_method_names_outside_object ] = true
1078
- 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 )
1079
1071
1080
1072
if model
1081
1073
model = _object_for_form_builder ( model )
@@ -1575,35 +1567,17 @@ def _object_for_form_builder(object) # :nodoc:
1575
1567
private
1576
1568
def html_options_for_form_with ( url_for_options = nil , model = nil , html : { } , local : !form_with_generates_remote_forms ,
1577
1569
skip_enforcing_utf8 : nil , **options )
1578
- html_options = options . slice ( :id , :class , :multipart , :method , :data ) . merge ( html )
1570
+ html_options = options . slice ( :id , :class , :multipart , :method , :data , :authenticity_token ) . merge! ( html )
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?
1581
-
1582
- html_options [ :enctype ] = "multipart/form-data" if html_options . delete ( :multipart )
1583
-
1584
- # The following URL is unescaped, this is just a hash of options, and it is the
1585
- # responsibility of the caller to escape all the values.
1586
- if url_for_options == false || html_options [ :action ] == false
1587
- html_options . delete ( :action )
1573
+ if skip_enforcing_utf8 . nil?
1574
+ if options . key? ( :enforce_utf8 )
1575
+ html_options [ :enforce_utf8 ] = options [ :enforce_utf8 ]
1576
+ end
1588
1577
else
1589
- html_options [ :action ] = url_for ( url_for_options || { } )
1578
+ html_options [ :enforce_utf8 ] = ! skip_enforcing_utf8
1590
1579
end
1591
- html_options [ :"accept-charset" ] = "UTF-8"
1592
- html_options [ :"data-remote" ] = true unless local
1593
-
1594
- html_options [ :authenticity_token ] = options . delete ( :authenticity_token )
1595
-
1596
- if !local && html_options [ :authenticity_token ] . blank?
1597
- html_options [ :authenticity_token ] = embed_authenticity_token_in_remote_forms
1598
- end
1599
-
1600
- if html_options [ :authenticity_token ] == true
1601
- # Include the default authenticity_token, which is only generated when it's set to nil,
1602
- # but we needed the true value to override the default of no authenticity_token on data-remote.
1603
- html_options [ :authenticity_token ] = nil
1604
- end
1605
-
1606
- html_options . stringify_keys!
1580
+ html_options_for_form ( url_for_options . nil? ? { } : url_for_options , html_options )
1607
1581
end
1608
1582
1609
1583
def instantiate_builder ( record_name , record_object , options )
0 commit comments