Skip to content

Commit bcb6c35

Browse files
committed
Call #html_options_for_form within form_with
The private [FormTagHelper#html_options_for_form][] helper covers the same logic as [FormHelper#html_options_for_form_with][]. This commit replaces the re-implementation within `#form_with` with a call to `#html_options_for_form`. In order to pass along the data in a correct shape, keep the `#html_options_for_form_with` method and use it to coerce the options. [FormTagHelper#html_options_for_form]: https://github.com/rails/rails/blob/fb1ab3460a676ce7def0819c2e92289ef2dcbe3b/actionview/lib/action_view/helpers/form_tag_helper.rb#L873-L894 [FormHelper#html_options_for_form_with]: https://github.com/rails/rails/blob/fb1ab3460a676ce7def0819c2e92289ef2dcbe3b/actionview/lib/action_view/helpers/form_helper.rb#L1552-L1579
1 parent 5a98545 commit bcb6c35

File tree

1 file changed

+4
-29
lines changed

1 file changed

+4
-29
lines changed

actionview/lib/action_view/helpers/form_helper.rb

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,7 @@ def apply_form_for_options!(record, object, options) # :nodoc:
757757
# form_with(**options.merge(builder: LabellingFormBuilder), &block)
758758
# end
759759
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
760+
options = { allow_method_names_outside_object: true, skip_default_ids: !form_with_generates_ids }.merge!(options)
762761

763762
if model
764763
if url != false
@@ -1575,35 +1574,11 @@ def _object_for_form_builder(object) # :nodoc:
15751574
private
15761575
def html_options_for_form_with(url_for_options = nil, model = nil, html: {}, local: !form_with_generates_remote_forms,
15771576
skip_enforcing_utf8: nil, **options)
1578-
html_options = options.slice(:id, :class, :multipart, :method, :data).merge(html)
1577+
html_options = options.slice(:id, :class, :multipart, :method, :data, :authenticity_token).merge!(html)
1578+
html_options[:remote] = !local
15791579
html_options[:method] ||= :patch if model.respond_to?(:persisted?) && model.persisted?
15801580
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)
1588-
else
1589-
html_options[:action] = url_for(url_for_options || {})
1590-
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!
1581+
html_options_for_form(url_for_options.nil? ? {} : url_for_options, html_options)
16071582
end
16081583

16091584
def instantiate_builder(record_name, record_object, options)

0 commit comments

Comments
 (0)