提交 92812ba0 编写于 作者: G Godfrey Chan

Merge pull request #13214 from JuanitoFatas/master

Some improvements on building nested forms. [ci skip]
...@@ -859,7 +859,7 @@ end ...@@ -859,7 +859,7 @@ end
This creates an `addresses_attributes=` method on `Person` that allows you to create, update and (optionally) destroy addresses. This creates an `addresses_attributes=` method on `Person` that allows you to create, update and (optionally) destroy addresses.
### Building the Form ### Nested Forms
The following form allows a user to create a `Person` and its associated addresses. The following form allows a user to create a `Person` and its associated addresses.
...@@ -882,16 +882,18 @@ The following form allows a user to create a `Person` and its associated address ...@@ -882,16 +882,18 @@ The following form allows a user to create a `Person` and its associated address
``` ```
When an association accepts nested attributes `fields_for` renders its block once for every element of the association. In particular, if a person has no addresses it renders nothing. A common pattern is for the controller to build one or more empty children so that at least one set of fields is shown to the user. The example below would result in 3 sets of address fields being rendered on the new person form. When an association accepts nested attributes `fields_for` renders its block once for every element of the association. In particular, if a person has no addresses it renders nothing. A common pattern is for the controller to build one or more empty children so that at least one set of fields is shown to the user. The example below would result in 2 sets of address fields being rendered on the new person form.
```ruby ```ruby
def new def new
@person = Person.new @person = Person.new
3.times { @person.addresses.build} 2.times { @person.addresses.build}
end end
``` ```
`fields_for` yields a form builder that names parameters in the format expected the accessor generated by `accepts_nested_attributes_for`. For example when creating a user with 2 addresses, the submitted parameters would look like The `fields_for` yields a form builder. The parameters' name will be what
`accepts_nested_attributes_for` expects. For example when creating a user with
2 addresses, the submitted parameters would look like:
```ruby ```ruby
{ {
...@@ -913,7 +915,7 @@ end ...@@ -913,7 +915,7 @@ end
The keys of the `:addresses_attributes` hash are unimportant, they need merely be different for each address. The keys of the `:addresses_attributes` hash are unimportant, they need merely be different for each address.
If the associated object is already saved, `fields_for` autogenerates a hidden input with the `id` of the saved record. You can disable this by passing `include_id: false` to `fields_for`. You may wish to do this if the autogenerated input is placed in a location where an input tag is not valid HTML or when using an ORM where children do not have an id. If the associated object is already saved, `fields_for` autogenerates a hidden input with the `id` of the saved record. You can disable this by passing `include_id: false` to `fields_for`. You may wish to do this if the autogenerated input is placed in a location where an input tag is not valid HTML or when using an ORM where children do not have an `id`.
### The Controller ### The Controller
...@@ -944,7 +946,9 @@ class Person < ActiveRecord::Base ...@@ -944,7 +946,9 @@ class Person < ActiveRecord::Base
end end
``` ```
If the hash of attributes for an object contains the key `_destroy` with a value of '1' or 'true' then the object will be destroyed. This form allows users to remove addresses: If the hash of attributes for an object contains the key `_destroy` with a value
of `1` or `true` then the object will be destroyed. This form allows users to
remove addresses:
```erb ```erb
<%= form_for @person do |f| %> <%= form_for @person do |f| %>
...@@ -952,7 +956,7 @@ If the hash of attributes for an object contains the key `_destroy` with a value ...@@ -952,7 +956,7 @@ If the hash of attributes for an object contains the key `_destroy` with a value
<ul> <ul>
<%= f.fields_for :addresses do |addresses_form| %> <%= f.fields_for :addresses do |addresses_form| %>
<li> <li>
<%= check_box :_destroy%> <%= addresses_form.check_box :_destroy%>
<%= addresses_form.label :kind %> <%= addresses_form.label :kind %>
<%= addresses_form.text_field :kind %> <%= addresses_form.text_field :kind %>
... ...
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册