Ruby on Rails - Railscasts PRO #196 Nested Model Form (revised)

Railscasts Reloaded · 24K views · 2014 · 11:16

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about Rails nested forms, especially dynamically adding and removing fields with fields_for and JavaScript.

Recommended by 4 people across 4 different months, 2015 to 2020.

What it is

A short Railscasts Pro episode (revised) showing how to build a single Rails form that edits several models using accepts_nested_attributes_for. It also covers adding and removing nested records dynamically with JavaScript, and is presented by Ryan Bates on the Railscasts Reloaded channel.

What people said

Recommends railsie · 2016-01 · Stack Overflow · on “Many-to-many complex forms”

This rails cast helped me set up a nested form so that a user could dynamically add and remove fields one at a time. [this video] . If you post the code you used when trying to set up your nested form I might be able to help you figure out why it didn't work.

Recommends Rajdeep Singh · 2016-12 · Stack Overflow · on “How to create or edit existing _form.html.rb partial from browser”

You need dynamic forms to achieve this, there's an awesome gem called nested_form [LINK] to easily created nested forms. The creator of this gem Ryan Bates has also created a Railscast session to demonstrate its usage and fortunately in that video the demo app is on survey. Here's the link to the video [this video] Here's the link to the gem [LINK] Hope that helps!

Recommends BenKoshy · 2020-03 · Stack Overflow · on “How to generate a new rails form when a previous form is submitted”

This is a common problem - it's all about nesting forms: the easiest option is to ensure that your recipe accepts nested attributes for [LINK] ingredients. Please watch this following railscast [this video] : it is actually quite easy to implement. The only catch is that the form is submitted all at once.

Recommends Amit Patel · 2020-05 · Stack Overflow · on “How to get each data from array which were saved by duplicated selectbox”

Couple of quick pointers. Replace `form_tag` with `form_for`. This will give you a form object and then on that form object you can do `form.fields_for` for `language` to build the relationship. Take a look at [LINK] and [this video]

Cites user5296896 · 2015-10 · Stack Overflow · on “Rails form using jQuery Delete”

I am trying to immediately delete part of my form when user clicks a link_to using jQuery and I've referred to [this video] Issue is, after deleting, the field is hidden however, if the user fails a validation or refresh the page, the field appears again. Only upon successful validation and the form is save then will the data be deleted. I wish to apply jQuery such that once the delete button is click, it is immediately deleted and even after a failed validation or user refresh the page, the section deleted will not appear again. The following is the code on the section i'm trying to delete: <fieldset> <%= f.check_box :mon %>Mon <%= f.check_box :tue %>Tue <%= f.check_box :wed %>Wed <%= f.check_bo

Cites Richard Peck · 2016-02 · Stack Overflow · on “Rails Nested Form - edit action & edit.html.erb won't display the form or related post content”

There are several problems with your structure: You don't have any fields in your form_for You're defining fields manually for fields_for ( BIG no no). form_for When you create a form_for , Rails uses a form object [LINK] to take data from your model , and output it into an HTML form. This output is done at render-time, and will not change regardless of how much manual lifting you do with javascript: <%= form_for @post do |f| %> # you need fields in here <%= f.submit %> <% end %> The way to debug this is to check the source of your page ( Right-Click > View Source ). If the <form> element is present, it means the form_for method is working: [LINK] Also, remember that form_for [LINK] is a hel

Critical Ilya Lapan · 2016-11 · Stack Overflow · on “Can't Understand Ruby On Rails Nested Model Form with JavaScript Tutorial”

I watched a tutorial for Nested Model Forms: [this video] I can't figure out what is going on there, to be honest, and I also ran into a few problems. For example: I tried implementing the code, and wrote the javascript described in the tutorial: $('form').on 'click', '.add_fields', (event) -> alert("----") time = new Date().getTime() regexp = new RegExp($(this).data('id'), 'g') $(this).before( $(this).data('fields').replace(regexp, time) ) event.preventDefault() If I understand correctly, this is inserting new form elements into the web page and replacing their id's with generated ones, so that webserver will be able to identify them. However, what happens, is that RegEx replaces ' id ' in 'h id