Libur Lebaran

Melihat pemandangan dari gedung-gedung tinggi ibu kota berubah menjadi hanya sesak kendaraan, bangunan jalan tol, sawah menghampar luas hijau, hingga gunung menjulang tinggi, dan akhirnya pemandangan…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Rails Nested Attributes

Rails give a strong mechanism to readily create rich forms called “nested attributes.” It enables you to combine more than one model in your forms while maintaining the same fundamental code pattern that you use with single model forms. Nested forms are forms that handle nested models and attributes in one form.

I have created a simple app that demonstrates rails nested attributes. In the app there are jobs and for each job, there can be one or more tasks associated with it. In rails, we can save/update a job along with its associated tasks in a single form.

You have to include Cocoon in your gemfile.

I will explain it with the help of an example. There are two models job and task.

Now we have a form for a job and in that form, we want to accept tasks for that job. The goal is that when we save/update the form for the job all tasks associated with that job are automatically saved. For doing that we have to first connect them in models like this. job.rb and task.rb looks like

Now in the form

In jobs controller, we have to change the new method to this

Done!

Add a comment

Related posts:

Why Nasty Opinions From Haters Are Fuel for Your Fire

Here are a few reasons why you should pay closer attention to the journey ahead if you want to move forward. No excuses allowed.

Why Alcohol Treatment Centers Are Vital and How to Find Them

Alcohol or drug treatment centers emerged first in the late nineteenth century. Even during the beginning of the twentieth century, they were still common. Back then they were called asylums. Now…

Cranes Rental Near Me Web Design Tips

Your cranes rental near me website is the ultimate visitor magnet; it is the primary contact point between the public and your company. Visitors trawl your web site thinking it will be pleasant; but…