-
Notifications
You must be signed in to change notification settings - Fork 21.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add form_with to unify form_tag/form_for. #26976
Changes from 1 commit
18a44be
c0df7c6
1e7e5cb
a4a5945
31a9608
3a55155
b714aa8
e3eb691
5a4052a
7d6fc1f
694c226
407d9f2
42f4fb2
15a805c
17429bb
30ebe5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
include_id
to skip_id
.
`skip_id: true` reads better than `include_id: false` (since the `include_id` default is true).
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -653,7 +653,7 @@ def apply_form_for_options!(record, object, options) #:nodoc: | |
# thus there is no primary key for comments. | ||
# | ||
# <%= form_with(model: @post) do |form| %> | ||
# <%= form.fields(:comments, include_id: false) do |fields| %> | ||
# <%= form.fields(:comments, skip_id: true) do |fields| %> | ||
# ... | ||
# <% end %> | ||
# <% end %> | ||
|
@@ -698,7 +698,7 @@ def form_with(model: nil, scope: nil, url: nil, format: nil, html: {}, remote: t | |
scope ||= model_name_from_record_or_class(model).param_key | ||
end | ||
|
||
html_options = html.merge(options.except(:index, :include_id, :builder)) | ||
html_options = html.merge(options.except(:index, :skip_id, :builder)) | ||
html_options[:method] ||= :patch if model.respond_to?(:persisted?) && model.persisted? | ||
html_options[:remote] = remote unless html_options.key?(:remote) | ||
|
||
|
@@ -1583,13 +1583,17 @@ def initialize(object_name, object, template, options) | |
@nested_child_index = {} | ||
@object_name, @object, @template, @options = object_name, object, template, options | ||
@default_options = @options ? @options.slice(:index, :namespace) : {} | ||
|
||
convert_to_legacy_options(@options) | ||
|
||
if @object_name.to_s.match(/\[\]$/) | ||
if (object ||= @template.instance_variable_get("@#{Regexp.last_match.pre_match}")) && object.respond_to?(:to_param) | ||
@auto_index = object.to_param | ||
else | ||
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 | ||
@index = options[:index] || options[:child_index] | ||
end | ||
|
@@ -1885,6 +1889,8 @@ def fields_for(record_name, record_object = nil, fields_options = {}, &block) | |
|
||
# See the docs for the <tt>ActionView::FormHelper.fields</tt> helper method. | ||
def fields(scope = nil, model: nil, **options, &block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this needs the same fixes from above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems all this doc stuff is repeated from above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, copied to mirror how |
||
convert_to_legacy_options(options) | ||
|
||
fields_for(scope || model, model, **options, &block) | ||
end | ||
|
||
|
@@ -2236,6 +2242,12 @@ def nested_child_index(name) | |
@nested_child_index[name] ||= -1 | ||
@nested_child_index[name] += 1 | ||
end | ||
|
||
def convert_to_legacy_options(options) | ||
if options.key?(:skip_id) | ||
options[:include_id] = !options[:skip_id] | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All this prep work is only used in the block_given? path, so it should be done there. Also, I think there's more we can do to make this a Composed Method. A lot of mechanics exposed at a high level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in the
else
too as the last parameter here too:html_options = html_options_for_form(url || {}, html_options)
But yeah, let me see what I can do with the composition 👍