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
As suggested by @dhh
> BTW, as far as "action" vs "active" goes, I'd like us to stick with this split: Action is for stuff that touches or talks to the user. Active is for internal.
> So imo, it should be ActionForm vs, say, ActiveModel
Set your models free from the `accepts_nested_attributes_for` helper. Active Form provides an object-oriented approach to represent your forms by building a form object, rather than relying on Active Record internals for doing this. Form objects provide an API to describe the models involved in the form, their attributes and validations. A form object deals with create/update actions of nested objects in a more seamless way.
5
+
Set your models free from the `accepts_nested_attributes_for` helper. Action Form provides an object-oriented approach to represent your forms by building a form object, rather than relying on Active Record internals for doing this. Form objects provide an API to describe the models involved in the form, their attributes and validations. A form object deals with create/update actions of nested objects in a more seamless way.
6
6
7
7
## Installation
8
8
9
9
Add this line to your `Gemfile`:
10
10
11
11
```ruby
12
-
gem 'active_form'
12
+
gem 'actionform'
13
13
```
14
14
15
15
## Defining Forms
16
16
17
17
Consider an example where you want to create/update a conference that can have many speakers which can present a single presentation with one form submission. You start by defining a form to represent the root model, `Conference`:
18
18
19
19
```ruby
20
-
classConferenceForm < ActiveForm::Base
20
+
classConferenceForm < ActionForm::Base
21
21
self.main_model =:conference
22
22
23
23
attributes :name, :city
@@ -26,7 +26,7 @@ class ConferenceForm < ActiveForm::Base
26
26
end
27
27
```
28
28
29
-
Your form object has to subclass `ActiveForm::Base` in order to gain the necessary API. When defining the form, you have to specify the main_model the form represents with the following line:
29
+
Your form object has to subclass `ActionForm::Base` in order to gain the necessary API. When defining the form, you have to specify the main_model the form represents with the following line:
30
30
31
31
```ruby
32
32
self.main_model =:conference
@@ -35,14 +35,14 @@ self.main_model = :conference
35
35
To add fields to the form, use the `attributes` or `attribute` class method. The form can also define validation rules for the model it represents. For the `presence` validation rule there is a short inline syntax:
36
36
37
37
```ruby
38
-
classConferenceForm < ActiveForm::Base
38
+
classConferenceForm < ActionForm::Base
39
39
attributes :name, :city, required:true
40
40
end
41
41
```
42
42
43
43
## The API
44
44
45
-
The `ActiveForm::Base` class provides a simple API with only a few instance/class methods. Below are listed the instance methods:
45
+
The `ActionForm::Base` class provides a simple API with only a few instance/class methods. Below are listed the instance methods:
46
46
47
47
1.`initialize(model)` accepts an instance of the model that the form represents.
48
48
2.`submit(params)` updates the main form's model and nested models with the posted parameters. The models are not saved/updated until you call `save`.
@@ -80,10 +80,10 @@ class ConferencesController
80
80
end
81
81
```
82
82
83
-
Active Form will read property values from the model in setup. Given the following form class.
83
+
Action Form will read property values from the model in setup. Given the following form class.
84
84
85
85
```ruby
86
-
classConferenceForm < ActiveForm::Base
86
+
classConferenceForm < ActionForm::Base
87
87
attribute :name
88
88
end
89
89
```
@@ -101,7 +101,7 @@ Your `@conference_form` is now ready to be rendered, either do it yourself or us
101
101
<% end %>
102
102
```
103
103
104
-
Nested forms and collections can be easily rendered with `fields_for`, etc. Just use Active Form as if it would be an Active Model instance in the view layer.
104
+
Nested forms and collections can be easily rendered with `fields_for`, etc. Just use Action Form as if it would be an Active Model instance in the view layer.
105
105
106
106
## Syncing Back
107
107
@@ -143,7 +143,7 @@ If the `save` method returns false due to validation errors defined on the form,
143
143
144
144
## Nesting Forms: 1-n Relations
145
145
146
-
Active Form also gives you nested collections.
146
+
Action Form also gives you nested collections.
147
147
148
148
Let's define the `has_many :speakers` collection association on the `Conference` model.
149
149
@@ -157,7 +157,7 @@ end
157
157
The form should look like this.
158
158
159
159
```ruby
160
-
classConferenceForm < ActiveForm::Base
160
+
classConferenceForm < ActionForm::Base
161
161
attributes :name, :city, required:true
162
162
163
163
association :speakersdo
@@ -172,7 +172,7 @@ This basically works like a nested `property` that iterates over a collection of
172
172
173
173
### has_many: Rendering
174
174
175
-
Active Form will expose the collection using the `speakers` method.
175
+
Action Form will expose the collection using the `speakers` method.
176
176
177
177
```erb
178
178
<%= form_for @conference_form |f| %>
@@ -201,7 +201,7 @@ end
201
201
The full form should look like this:
202
202
203
203
```ruby
204
-
classConferenceForm < ActiveForm::Base
204
+
classConferenceForm < ActionForm::Base
205
205
attributes :name, :city, required:true
206
206
207
207
association :speakersdo
@@ -237,12 +237,12 @@ Use `fields_for` in a Rails environment to correctly setup the structure of para
237
237
238
238
## Dynamically Adding/Removing Nested Objects
239
239
240
-
Active Form comes with two helpers to deal with this functionality:
240
+
Action Form comes with two helpers to deal with this functionality:
241
241
242
242
1.`link_to_add_association` will display a link that renders fields to create a new object.
243
243
2.`link_to_remove_association` will display a link to remove a existing/dynamic object.
244
244
245
-
In order to use it you have to insert this line: `//= require active_form` to your `app/assets/javascript/application.js` file.
245
+
In order to use it you have to insert this line: `//= require action_form` to your `app/assets/javascript/application.js` file.
246
246
247
247
In our `ConferenceForm` we can dynamically create/remove `Speaker` objects. To do that we would write in the `app/views/conferences/_form.html.erb` partial:
248
248
@@ -324,7 +324,7 @@ And `app/views/conferences/_presentation_fields.html.erb` would be:
324
324
325
325
## Plain Old Ruby Object Forms
326
326
327
-
ActiveForm also can accept `ActiveModel::Model` instances as a model.
327
+
ActionForm also can accept `ActiveModel::Model` instances as a model.
0 commit comments