@@ -20,9 +20,9 @@ Consider an example where you want to create/update a conference that can have m
2020``` ruby
2121class ConferenceForm < ActiveForm ::Base
2222 self .main_model = :conference
23-
23+
2424 attributes :name , :city
25-
25+
2626 validates :name , :city , presence: true
2727end
2828```
@@ -223,11 +223,11 @@ Use `fields_for` in a Rails environment to correctly setup the structure of para
223223<%= form_for @conference_form |f| %>
224224 <%= f.text_field :name %>
225225 <%= f.text_field :city %>
226-
226+
227227 <%= f.fields_for :speakers do |s| %>
228228 <%= s.text_field :name %>
229229 <%= s.text_field :occupation %>
230-
230+
231231 <%= s.fields_for :presentation do |p| %>
232232 <%= p.text_field :topic %>
233233 <%= p.text_field :duration %>
@@ -323,6 +323,49 @@ And `app/views/conferences/_presentation_fields.html.erb` would be:
323323</div>
324324```
325325
326+ ## Plain Old Ruby Object Forms
327+
328+ ActiveForm also can accept ` ActiveModel::Model ` instances as a model.
329+
330+ Let's define the Feeback class class with ` ActiveModel::Model ` that will be used for customer's feedback:
331+
332+ ``` ruby
333+ class Feedback
334+ include ActiveModel ::Model
335+
336+ attr_accessible :name , :body , :email
337+
338+ def save
339+ FeedbackMailer .send_email(email, name, body)
340+ end
341+ end
342+ ```
343+
344+ The form should look like this.
345+
346+ ``` ruby
347+ class FeedbackForm < ActiveForm ::Base
348+ attributes :name , :body , :email , required: true
349+ end
350+ ```
351+
352+ And then in controller:
353+
354+ ``` ruby
355+ class FeedbacksController
356+ def create
357+ feedback = Feedback .new
358+ @feedback_form = FeedbackForm .new (feedback)
359+ @feedback_form .submit(feedback_params)
360+
361+ if @feedback_form .save
362+ head :ok
363+ else
364+ render json: @feedback_form .errors
365+ end
366+ end
367+ ```
368+
326369## Demos
327370
328371You can find a list of applications using this gem in this repository: https://github.com/m-Peter/nested-form-examples .
0 commit comments