aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/nested_model_forms.textile
diff options
context:
space:
mode:
authorEloy Duran <eloy.de.enige@gmail.com>2009-03-15 16:21:56 +0100
committerEloy Duran <eloy.de.enige@gmail.com>2009-03-15 16:22:14 +0100
commit3b89aa605c543e772678e03add1818520c02bec8 (patch)
tree0140516257408e05a140bea6c4e5c0b84068f9e8 /railties/guides/source/nested_model_forms.textile
parent13cc934cc8510aef0aa3989c118996bc508a985f (diff)
downloadrails-3b89aa605c543e772678e03add1818520c02bec8.tar.gz
rails-3b89aa605c543e772678e03add1818520c02bec8.tar.bz2
rails-3b89aa605c543e772678e03add1818520c02bec8.zip
Added a collection association form example.
Diffstat (limited to 'railties/guides/source/nested_model_forms.textile')
-rw-r--r--railties/guides/source/nested_model_forms.textile47
1 files changed, 46 insertions, 1 deletions
diff --git a/railties/guides/source/nested_model_forms.textile b/railties/guides/source/nested_model_forms.textile
index e975b97ea8..4b685b214e 100644
--- a/railties/guides/source/nested_model_forms.textile
+++ b/railties/guides/source/nested_model_forms.textile
@@ -174,4 +174,49 @@ When this form is posted the Rails parameter parser will construct a hash like t
}
</ruby>
-That’s it. The controller will simply pass this hash on to the model from the +create+ action. The model will then handle building the +address+ association for you and automatically be saved when the parent (+person+) is saved. \ No newline at end of file
+That’s it. The controller will simply pass this hash on to the model from the +create+ action. The model will then handle building the +address+ association for you and automatically save it when the parent (+person+) is saved.
+
+h5. Nested form for a collection of associated objects
+
+The form code for an association collection is pretty similar to that of a single associated object:
+
+<erb>
+<% form_for @person do |f| %>
+ <%= f.text_field :name %>
+
+ <% f.fields_for :projects do |pf| %>
+ <%= f.text_field :name %>
+ <% end %>
+<% end %>
+</erb>
+
+Which generates:
+
+<html>
+<form action="/people" class="new_person" id="new_person" method="post">
+ <input id="person_name" name="person[name]" size="30" type="text" />
+
+ <input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" size="30" type="text" />
+ <input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" size="30" type="text" />
+</form>
+</html>
+
+As you can see it has generated 2 +project name+ inputs, one for each new +project+ that’s build in the controllers +new+ action. Only this time the +name+ attribute of the input contains a digit as an extra namespace. This will be parsed by the Rails parameter parser as:
+
+<ruby>
+{
+ "person" => {
+ "name" => "Eloy Duran",
+ "projects_attributes" => {
+ "0" => { "name" => "Project 1" },
+ "1" => { "name" => "Project 2" }
+ }
+ }
+}
+</ruby>
+
+You can basically see the +projects_attributes+ hash as an array of attribute hashes. One for each model instance.
+
+NOTE: The reason that +fields_for+ constructed a form which would result in a hash instead of an array is that it won't work for any forms nested deeper than one level deep.
+
+TIP: You _can_ however pass an array to the writer method generated by +accepts_nested_attributes_for+ if you're using plain Ruby or some other API access. See (TODO) for more info and example. \ No newline at end of file