googleads
Nested form fields implementation in Active Model | Agira Technologies
preloder
API Technical Web Development

Nested form fields implementation in Active Model

Introduction

In the ActiveRecord model, ‘has_many’ and ‘accepts_nested_attributes_for’ are default components to build form objects so as to create multiple records.
ActiveModel::Model is an excellent way to make objects behave like ActiveRecord. But the ActiveModel lacks one key feature, which is the ‘accepts_nested_attributes_for’. As we know that this is a primary attribute for nested fields, in this article, we will see how to implement nested form fields in ActiveModel, i.e without ActiveRecord.

Why we use ActiveModel?

If we want to make use of form data that doesn’t necessarily persist to an object or used for other non-active record purposes like API data set, Active Model Helper Modules come into role. They reduce a lot of  complexity.
For example, the Data layer(API – data node) is running in a separate node(API node) and its built in ActiveRecord/Sequel ORM and UI is running in a separate node. This uses the ActiveModel, because the data does not persist in the UI layer. Here we are using ActiveModel to make the UI controller clean and to build a form object in an easier way.
In the UI layer, when we try to build form objects to create multiple records (Eg: An organization having multiple recruitment steps), ActiveModel doesn’t have ‘accepts_nested_attributes_for’ feature to implement this.
To resolve this problem, initially we looked out for gems, but after a while, we found the perfect solution.

Step 1: Defining form Objects in Model

If an object with a one-to-many association is instantiated with a params hash, and that hash has a key for the association, Rails will call the <association_name>_attributes= method on that object. So, we need to define our form object in Model.

 

Step 2: Handling in controller

In controller action, we are trying to build organization recruitment object based on two conditions.
● Condition 1: Organization already has some recruitments, and so the object built is as below:

 

● Condition 2: Object doesn’t have any recruitments yet, and so the object built is as below:

Put together, the whole method looks like below:

 

Step 3: Handling in view

Now we need a form for this model. If Object doesn’t have any recruitments yet (condition 2), we need to make sure that the form has at least one ‘fields_for’ block to render, by giving it one on initialization.

 

Conclusion

This is how we implement nested form fields using ActiveModel and without Active Record. This produces the desired results to us using ActiveModel, just as we would have obtained if we used ActiveRecord, using ‘accepts_nested_attributes_for’… And we are good to go!

The following two tabs change content below.

Bharanidharan Arumugam

Technical Architect | Tech Lead | Mentor | - An enthusiastic & vibrant Full stack developer has 7 plus years of experience in Web development arena. Owns legitimate knowledge in Ruby, Ruby On Rails, AngularJs, NodeJs, MEAN Stack, CMS Services. Apart all, A Modest human who strictly says "No To Harming Humans".

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call