Skip to content

Commit 4e6225d

Browse files
authored
Merge pull request rails#51704 from notapatch/pr-prefer-form-with
[ci skip]Docs highlight form_with over form_for
2 parents 371f9ad + aabcb47 commit 4e6225d

File tree

8 files changed

+37
-36
lines changed

8 files changed

+37
-36
lines changed

actionpack/lib/action_controller/form_builder.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ module ActionController
2222
# default_form_builder AdminFormBuilder
2323
# end
2424
#
25-
# Then in the view any form using `form_for` will be an instance of the
26-
# specified form builder:
25+
# Then in the view any form using `form_with` or `form_for` will be an
26+
# instance of the specified form builder:
2727
#
28-
# <%= form_for(@instance) do |builder| %>
28+
# <%= form_with(model: @instance) do |builder| %>
2929
# <%= builder.special_field(:name) %>
3030
# <% end %>
3131
module FormBuilder

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,8 +2229,8 @@ def direct(name, options = {}, &block)
22292229
end
22302230

22312231
# Define custom polymorphic mappings of models to URLs. This alters the behavior
2232-
# of `polymorphic_url` and consequently the behavior of `link_to` and `form_for`
2233-
# when passed a model instance, e.g:
2232+
# of `polymorphic_url` and consequently the behavior of `link_to`, `form_with`
2233+
# and `form_for` when passed a model instance, e.g:
22342234
#
22352235
# resource :basket
22362236
#
@@ -2239,7 +2239,7 @@ def direct(name, options = {}, &block)
22392239
# end
22402240
#
22412241
# This will now generate "/basket" when a `Basket` instance is passed to
2242-
# `link_to` or `form_for` instead of the standard "/baskets/:id".
2242+
# `link_to`, `form_with` or `form_for` instead of the standard "/baskets/:id".
22432243
#
22442244
# NOTE: This custom behavior only applies to simple polymorphic URLs where a
22452245
# single model instance is passed and not more complicated forms, e.g:

actionpack/lib/action_dispatch/routing/polymorphic_routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module Routing
3131
# * `url_for`, so you can use it with a record as the argument, e.g.
3232
# `url_for(@article)`;
3333
# * ActionView::Helpers::FormHelper uses `polymorphic_path`, so you can write
34-
# `form_for(@article)` without having to specify `:url` parameter for the
34+
# `form_with(model: @article)` without having to specify `:url` parameter for the
3535
# form action;
3636
# * `redirect_to` (which, in fact, uses `url_for`) so you can write
3737
# `redirect_to(post)` in your controllers;
@@ -61,7 +61,7 @@ module Routing
6161
# argument to the method. For example:
6262
#
6363
# polymorphic_url([blog, @post]) # calls blog.post_path(@post)
64-
# form_for([blog, @post]) # => "/blog/posts/1"
64+
# form_with(model: [blog, @post]) # => "/blog/posts/1"
6565
#
6666
module PolymorphicRoutes
6767
# Constructs a call to a named RESTful route for the given record and returns

actionview/lib/action_view/helpers/date_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ def separator(type)
12281228
class FormBuilder
12291229
# Wraps ActionView::Helpers::DateHelper#date_select for form builders:
12301230
#
1231-
# <%= form_for @person do |f| %>
1231+
# <%= form_with model: @person do |f| %>
12321232
# <%= f.date_select :birth_date %>
12331233
# <%= f.submit %>
12341234
# <% end %>
@@ -1240,7 +1240,7 @@ def date_select(method, options = {}, html_options = {})
12401240

12411241
# Wraps ActionView::Helpers::DateHelper#time_select for form builders:
12421242
#
1243-
# <%= form_for @race do |f| %>
1243+
# <%= form_with model: @race do |f| %>
12441244
# <%= f.time_select :average_lap %>
12451245
# <%= f.submit %>
12461246
# <% end %>
@@ -1252,7 +1252,7 @@ def time_select(method, options = {}, html_options = {})
12521252

12531253
# Wraps ActionView::Helpers::DateHelper#datetime_select for form builders:
12541254
#
1255-
# <%= form_for @person do |f| %>
1255+
# <%= form_with model: @person do |f| %>
12561256
# <%= f.datetime_select :last_request_at %>
12571257
# <%= f.submit %>
12581258
# <% end %>

actionview/lib/action_view/helpers/form_helper.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,21 @@ module Helpers # :nodoc:
3030
# when the form is initially displayed, input fields corresponding to attributes
3131
# of the resource should show the current values of those attributes.
3232
#
33-
# In \Rails, this is usually achieved by creating the form using +form_for+ and
34-
# a number of related helper methods. +form_for+ generates an appropriate <tt>form</tt>
35-
# tag and yields a form builder object that knows the model the form is about.
36-
# Input fields are created by calling methods defined on the form builder, which
37-
# means they are able to generate the appropriate names and default values
33+
# In \Rails, this is usually achieved by creating the form using either
34+
# +form_with+ or +form_for+ and a number of related helper methods. These
35+
# methods generate an appropriate <tt>form</tt> tag and yield a form
36+
# builder object that knows the model the form is about. Input fields are
37+
# created by calling methods defined on the form builder, which means they
38+
# are able to generate the appropriate names and default values
3839
# corresponding to the model attributes, as well as convenient IDs, etc.
39-
# Conventions in the generated field names allow controllers to receive form data
40-
# nicely structured in +params+ with no effort on your side.
40+
# Conventions in the generated field names allow controllers to receive form
41+
# data nicely structured in +params+ with no effort on your side.
4142
#
4243
# For example, to create a new person you typically set up a new instance of
4344
# +Person+ in the <tt>PeopleController#new</tt> action, <tt>@person</tt>, and
44-
# in the view template pass that object to +form_for+:
45+
# in the view template pass that object to +form_with+ or +form_for+:
4546
#
46-
# <%= form_for @person do |f| %>
47+
# <%= form_with model: @person do |f| %>
4748
# <%= f.label :first_name %>:
4849
# <%= f.text_field :first_name %><br />
4950
#
@@ -783,9 +784,9 @@ def form_with(model: false, scope: nil, url: nil, format: nil, **options, &block
783784
end
784785
end
785786

786-
# Creates a scope around a specific model object like form_with, but
787-
# doesn't create the form tags themselves. This makes fields_for suitable
788-
# for specifying additional model objects in the same form.
787+
# Creates a scope around a specific model object like +form_with+, but
788+
# doesn't create the form tags themselves. This makes +fields_for+
789+
# suitable for specifying additional model objects in the same form.
789790
#
790791
# Although the usage and purpose of +fields_for+ is similar to +form_with+'s,
791792
# its method signature is slightly different. Like +form_with+, it yields
@@ -2032,9 +2033,9 @@ def field_name(method, *methods, multiple: false, index: @options[:index])
20322033
end
20332034
alias_method :text_area, :textarea
20342035

2035-
# Creates a scope around a specific model object like form_with, but
2036-
# doesn't create the form tags themselves. This makes fields_for suitable
2037-
# for specifying additional model objects in the same form.
2036+
# Creates a scope around a specific model object like +form_with+, but
2037+
# doesn't create the form tags themselves. This makes +fields_for+
2038+
# suitable for specifying additional model objects in the same form.
20382039
#
20392040
# Although the usage and purpose of +fields_for+ is similar to +form_with+'s,
20402041
# its method signature is slightly different. Like +form_with+, it yields

actionview/lib/action_view/helpers/form_options_helper.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ def prompt_text(prompt)
840840
class FormBuilder
841841
# Wraps ActionView::Helpers::FormOptionsHelper#select for form builders:
842842
#
843-
# <%= form_for @post do |f| %>
843+
# <%= form_with model: @post do |f| %>
844844
# <%= f.select :person_id, Person.all.collect { |p| [ p.name, p.id ] }, include_blank: true %>
845845
# <%= f.submit %>
846846
# <% end %>
@@ -852,7 +852,7 @@ def select(method, choices = nil, options = {}, html_options = {}, &block)
852852

853853
# Wraps ActionView::Helpers::FormOptionsHelper#collection_select for form builders:
854854
#
855-
# <%= form_for @post do |f| %>
855+
# <%= form_with model: @post do |f| %>
856856
# <%= f.collection_select :person_id, Author.all, :id, :name_with_initial, prompt: true %>
857857
# <%= f.submit %>
858858
# <% end %>
@@ -864,7 +864,7 @@ def collection_select(method, collection, value_method, text_method, options = {
864864

865865
# Wraps ActionView::Helpers::FormOptionsHelper#grouped_collection_select for form builders:
866866
#
867-
# <%= form_for @city do |f| %>
867+
# <%= form_with model: @city do |f| %>
868868
# <%= f.grouped_collection_select :country_id, @continents, :countries, :name, :id, :name %>
869869
# <%= f.submit %>
870870
# <% end %>
@@ -876,7 +876,7 @@ def grouped_collection_select(method, collection, group_method, group_label_meth
876876

877877
# Wraps ActionView::Helpers::FormOptionsHelper#time_zone_select for form builders:
878878
#
879-
# <%= form_for @user do |f| %>
879+
# <%= form_with model: @user do |f| %>
880880
# <%= f.time_zone_select :time_zone, nil, include_blank: true %>
881881
# <%= f.submit %>
882882
# <% end %>
@@ -888,7 +888,7 @@ def time_zone_select(method, priority_zones = nil, options = {}, html_options =
888888

889889
# Wraps ActionView::Helpers::FormOptionsHelper#weekday_select for form builders:
890890
#
891-
# <%= form_for @user do |f| %>
891+
# <%= form_with model: @user do |f| %>
892892
# <%= f.weekday_select :weekday, include_blank: true %>
893893
# <%= f.submit %>
894894
# <% end %>
@@ -900,7 +900,7 @@ def weekday_select(method, options = {}, html_options = {})
900900

901901
# Wraps ActionView::Helpers::FormOptionsHelper#collection_checkboxes for form builders:
902902
#
903-
# <%= form_for @post do |f| %>
903+
# <%= form_with model: @post do |f| %>
904904
# <%= f.collection_checkboxes :author_ids, Author.all, :id, :name_with_initial %>
905905
# <%= f.submit %>
906906
# <% end %>
@@ -913,7 +913,7 @@ def collection_checkboxes(method, collection, value_method, text_method, options
913913

914914
# Wraps ActionView::Helpers::FormOptionsHelper#collection_radio_buttons for form builders:
915915
#
916-
# <%= form_for @post do |f| %>
916+
# <%= form_with model: @post do |f| %>
917917
# <%= f.collection_radio_buttons :author_id, Author.all, :id, :name_with_initial %>
918918
# <%= f.submit %>
919919
# <% end %>

actionview/lib/action_view/record_identifier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module ActionView
1111
#
1212
# Consider for example the following code that form of post:
1313
#
14-
# <%= form_for(post) do |f| %>
14+
# <%= form_with(model: post) do |f| %>
1515
# <%= f.text_field :body %>
1616
# <% end %>
1717
#

railties/lib/rails/engine.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ module Rails
245245
# polymorphic_url(MyEngine::Article.new)
246246
# # => "articles_path" # not "my_engine_articles_path"
247247
#
248-
# form_for(MyEngine::Article.new) do
248+
# form_with(model: MyEngine::Article.new) do
249249
# text_field :title # => <input type="text" name="article[title]" id="article_title" />
250250
# end
251251
#
@@ -294,7 +294,7 @@ module Rails
294294
# All you need to do is pass the helper as the first element in array with
295295
# attributes for URL:
296296
#
297-
# form_for([my_engine, @user])
297+
# form_with(model: [my_engine, @user])
298298
#
299299
# This code will use <tt>my_engine.user_path(@user)</tt> to generate the proper route.
300300
#

0 commit comments

Comments
 (0)