Skip to content

Conversation

prathamesh-sonpatki
Copy link
Member

@prathamesh-sonpatki
Copy link
Member Author

@@ -774,7 +774,7 @@ 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).
Copy link
Member

Choose a reason for hiding this comment

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

can you wrap this line to 80 chars?

Copy link
Member Author

Choose a reason for hiding this comment

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

@senny Done!

- fields_for documentation with index option was wrong.
- It does not work with passing model as it is.
- Changed the example by passing id of the address object.
- Fixes rails#13125.
@senny
Copy link
Member

senny commented Jan 14, 2014

looks good. Thanks.

senny added a commit that referenced this pull request Jan 14, 2014
Fix fields_for documentation with index option [ci skip]
@senny senny merged commit c2a6fc4 into rails:master Jan 14, 2014
senny added a commit that referenced this pull request Jan 14, 2014
Fix fields_for documentation with index option [ci skip]
@prathamesh-sonpatki prathamesh-sonpatki deleted the issue-13125 branch January 14, 2014 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fields_for does not work with model :index
2 participants