aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-01-14 01:16:34 -0800
committerYves Senn <yves.senn@gmail.com>2014-01-14 01:16:34 -0800
commitc2a6fc42d2bed0558f74d61dab72e6eceea3f57c (patch)
tree5b3dc2ea7548b9dbb1d5caef620d9677df633a4e
parentd901699acac572a7c3b5f3de45d8f59e83d0db2a (diff)
parent3cf5d2b75bca5eb0578643780bfeb50e75ba59be (diff)
downloadrails-c2a6fc42d2bed0558f74d61dab72e6eceea3f57c.tar.gz
rails-c2a6fc42d2bed0558f74d61dab72e6eceea3f57c.tar.bz2
rails-c2a6fc42d2bed0558f74d61dab72e6eceea3f57c.zip
Merge pull request #13706 from prathamesh-sonpatki/issue-13125
Fix fields_for documentation with index option [ci skip]
-rw-r--r--guides/source/form_helpers.md13
1 files changed, 10 insertions, 3 deletions
diff --git a/guides/source/form_helpers.md b/guides/source/form_helpers.md
index ec4a255398..455dc7bebe 100644
--- a/guides/source/form_helpers.md
+++ b/guides/source/form_helpers.md
@@ -751,7 +751,7 @@ You might want to render a form with a set of edit fields for each of a person's
<%= form_for @person do |person_form| %>
<%= person_form.text_field :name %>
<% @person.addresses.each do |address| %>
- <%= person_form.fields_for address, index: address do |address_form|%>
+ <%= person_form.fields_for address, index: address.id do |address_form|%>
<%= address_form.text_field :city %>
<% end %>
<% end %>
@@ -774,9 +774,16 @@ This will result in a `params` hash that looks like
{'person' => {'name' => 'Bob', 'address' => {'23' => {'city' => 'Paris'}, '45' => {'city' => 'London'}}}}
```
-Rails knows that all these inputs should be part of the person hash because you called `fields_for` on the first form builder. By specifying an `:index` option you're telling Rails that instead of naming the inputs `person[address][city]` it should insert that index surrounded by [] between the address and the city. If you pass an Active Record object as we did then Rails will call `to_param` on it, which by default returns the database id. This is often useful as it is then easy to locate which Address record should be modified. You can pass numbers with some other significance, strings or even `nil` (which will result in an array parameter being created).
+Rails knows that all these inputs should be part of the person hash because you
+called `fields_for` on the first form builder. By specifying an `:index` option
+you're telling Rails that instead of naming the inputs `person[address][city]`
+it should insert that index surrounded by [] between the address and the city.
+This is often useful as it is then easy to locate which Address record
+should be modified. You can pass numbers with some other significance,
+strings or even `nil` (which will result in an array parameter being created).
-To create more intricate nestings, you can specify the first part of the input name (`person[address]` in the previous example) explicitly, for example
+To create more intricate nestings, you can specify the first part of the input
+name (`person[address]` in the previous example) explicitly:
```erb
<%= fields_for 'person[address][primary]', address, index: address do |address_form| %>