You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Matestack provides functionality for reactive nested forms.
4
8
5
9
This works in conjunction with rails' `accepts_nested_attributes_for`. From the rails documentation on [nested attributes](https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html):
6
10
7
-
> Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute updating is turned off and you can enable it using the accepts_nested_attributes_for class method. When you enable nested attributes an attribute writer is defined on the model.
8
-
9
-
There is a little bit of setup required to enable this. There's a need for `accepts_nested_attributes_for`, `index_errors` on a models' `has_many` associations and an ActiveRecord patch.
11
+
> Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute updating is turned off and you can enable it using the accepts\_nested\_attributes\_for class method. When you enable nested attributes an attribute writer is defined on the model.
10
12
13
+
There is a little bit of setup required to enable this. There's a need for `accepts_nested_attributes_for`, `index_errors` on a models' `has_many` associations and an ActiveRecord patch.
11
14
12
-
Consider the following model setup, which is the same model found in the dummy app in the spec directory (active in this dummy app):
15
+
Consider the following model setup, which is the same model found in the dummy app in the spec directory \(active in this dummy app\):
13
16
14
17
```ruby
15
18
classDummyModel < ApplicationRecord
@@ -29,14 +32,13 @@ Note the `has_many :dummy_child_models, index_errors: true` declaration in the `
29
32
30
33
Normally with rails, when rendering forms using Active Record models, errors are available on individual model instances. When using `accepts_nested_attributes_for`, error messages sent as JSON are not as useful because it is not possible to figure out which associated model object the error relates to.
31
34
32
-
From rails 5, we can add an index to errors on nested models. We can add the option `index_errors: true` to has_many association to enable this behaviour on individual association.
33
-
35
+
From rails 5, we can add an index to errors on nested models. We can add the option `index_errors: true` to has\_many association to enable this behaviour on individual association.
34
36
35
37
## ActiveRecord Patch
36
38
37
39
Matestack nested forms support requires an ActiveRecord patch. This is because `index_errors` does not consider indexes of the correct existing sub records.
38
40
39
-
See rails [issue #24390](https://github.com/rails/rails/issues/24390)
41
+
See rails [issue \#24390](https://github.com/rails/rails/issues/24390)
40
42
41
43
Add this monkey patch to your rails app
42
44
@@ -58,76 +60,96 @@ module ActiveRecord
58
60
end
59
61
```
60
62
63
+
## Controller Setup
64
+
65
+
Adjusting strong params for nested form support does not differ from what needs to be done when using classsic Rails forms, e.g.:
# type: :button is important! otherwise form submission may be triggered when removing an item.
125
+
button "remove", type::button
107
126
end
108
127
end
109
128
end
110
129
end
111
130
```
112
131
113
-
### Dynamically Adding Nested Items
132
+
### Dynamically Adding Nested Items\(Optional\)
114
133
115
-
As in the example above, you can dynamically add nested items. As the comment in the code suggests `type: :button` is important, otherwise remove on first item is triggered on enter.
134
+
As in the example above, you can dynamically add nested items. As the comment in the code suggests `type: :button` is important, otherwise form submission may be triggered when removing an item.
116
135
117
136
```ruby
137
+
# lives outside of a form_fields_for component!
118
138
form_fields_for_add_item key::dummy_child_models_attributes, prototype: method(:dummy_child_model_form) do
119
-
# type: :button is important! otherwise remove on first item is triggered on enter
120
-
button "add", type::button
139
+
# type: :button is important! otherwise form submission may be triggered when removing an item
140
+
button "add", type::button
121
141
end
122
142
```
123
143
124
-
### Dynamically Removing Nested Items
144
+
### Dynamically Removing Nested Items\(Optional\)
125
145
126
-
As in the example above, as well as dynamically adding items, you can dynamically remove nested items. Again, important: `type: :button` is important, otherwise remove on first item is triggered on enter.
146
+
As in the example above, as well as dynamically adding items, you can dynamically remove nested items. Again, important: `type: :button` is important, otherwise form submission may be triggered when adding an item.
127
147
128
148
```ruby
149
+
# has to be placed within a form_fields_for component!
129
150
form_fields_for_remove_item do
130
-
#id is just required in this spec, but type: :button is important! otherwise remove on first item is triggered on enter
0 commit comments