Form Nested Attributes and Fields For in Rails

GoRails · 25K views · 2015 · 9:19

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering Rails questions about saving parent and child records from a single form with nested attributes.

Recommended by 2 people across 2 different months, 2016 to 2022.

What it is

A short GoRails screencast showing how to build nested forms in Rails using accepts_nested_attributes_for and fields_for. It walks through setting up the model association and form so a parent record and its child records are created together from one form.

What people said

Recommends Maxeezy · 2020-03 · Stack Overflow · on “Rails 5: Submitting multiple forms with one submit button best practice”

I actually found out that both are not applicable and the answer to my question was server-side rendering with fields_for . Before I explain fields_for I want to answer why my strategies from above are not relevant. Submit two forms with one button [LINK] works, but renders everything client-side. That means it worked on my local machine, but as soon as I deployed the app on heroku and tried it, the javascript would not save all the records because the browser would terminate the JS after the first (or the first few) submit() actions. is just a service to outsource workload. That would not solve the problem of inconsistent database records. So I needed to find a way to process the records se

Recommends GabrielTheCoder · 2022-09 · Stack Overflow · on “How do i create a parent and child element at the same time rails”

After following the GoRails screencast [this video] on how to properly set nested form attributes in rails, I still came across errors. I eventually could trace them and found this neat post [LINK] which game the hint to use inverse_of and autosave: true . I am not 100% sure what those do, even though I will read now to find out, but my stuff is working properly now :) Modified Model class Invoice < ApplicationRecord belongs_to :project has_many :invoice_items, inverse_of: :invoice, autosave: true accepts_nested_attributes_for :invoice_items ...

Cites mr. jman · 2016-03 · Stack Overflow · on “Rails 4 Nested Forms, Error: Unpermitted parameter: order_item”

The view that rendered the form was not in another controller and not in the orders controller. The show action of tht view did not include an instance variable for order_items . This caused the error messages. Thank you for all of your help. For anyone looking for resources for nested forms below are some helpful resources. [this video] API dock for fields_for [LINK] Go Rails video for Form Nested Attributes and Fields For in Rails [LINK] Active Record Nested Attributes [LINK] Rails Guide: Active Record Associations [LINK]