Skip to content
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

Merged
merged 16 commits into from
Nov 21, 2016
Merged
Prev Previous commit
Next Next commit
Add fields DSL method.
Strips `_for` and requires models passed as a keyword argument.
  • Loading branch information
kaspth committed Nov 6, 2016
commit 1e7e5cb8f21b45ce3b031b6dc56663a0a81dfa4c
18 changes: 17 additions & 1 deletion actionview/lib/action_view/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,17 @@ def fields_for(record_name, record_object = nil, options = {}, &block)
capture(builder, &block)
end

# TODO: Documentation
def fields(scope = nil, model: nil, **options, &block)
# TODO: Remove when ids and classes are no longer output by default.
if model
scope ||= model_name_from_record_or_class(model).param_key
end

builder = instantiate_builder(scope, model, options)
capture(builder, &block)
end

# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
# is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly.
Expand Down Expand Up @@ -1314,7 +1325,7 @@ class FormBuilder

# The methods which wrap a form helper call.
class_attribute :field_helpers
self.field_helpers = [:fields_for, :label, :text_field, :password_field,
self.field_helpers = [:fields_for, :fields, :label, :text_field, :password_field,
:hidden_field, :file_field, :text_area, :check_box,
:radio_button, :color_field, :search_field,
:telephone_field, :phone_field, :date_field,
Expand Down Expand Up @@ -1651,6 +1662,11 @@ def fields_for(record_name, record_object = nil, fields_options = {}, &block)
@template.fields_for(record_name, record_object, fields_options, &block)
end

# TODO: Documentation
def fields(scope = nil, model: nil, **options, &block)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this needs the same fixes from above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems all this doc stuff is repeated from above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, copied to mirror how fields_for documents itself (i.e. both as the helper and in the builder). I'll update this to just refer to the fields helper docs above.

fields_for(scope || model, model, **options, &block)
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhh added the fields method to do:

form_with(model: @post) do |form|
  form.fields(:comment, model: @comment) do |fields|
    fields.text_field # ...
  end
end

But it reads a bit weird if the passed model is a collection: fields(:comments, model: @post.comments).

Not sure if you intended this to be much more than a fields_for alias with the don't output ids and classes by default config.


# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
# is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly.
Expand Down
Loading