Dynamic Nested Forms With Turbo Part 2 Discussion
Great lesson: thanks Collin! I learned a couple of new tricks there - appreciated!
Hey Terry! Thanks so much for the kind words and I’m super glad to hear that you enjoyed it and got some new tricks out of it!
For some reason, I lost the ability to update after adding the helper at the end. Anyone else?
Unpermitted parameters: :task_12, :task_13. Context: { controller: ProjectsController, action: update, request: #<ActionDispatch::Request:0x000000010f9478b0>, params: {"_method"=>"patch", "authenticity_token"=>"[FILTERED]", "project"=>{"name"=>"something", "tasks_attributes"=>{"task_12"=>{"description"=>"text"}, "task_13"=>{"description"=>"text",}}}, "button"=>"", "controller"=>"projects", "action"=>"update", "id"=>"2"} }
Well, looked into it further. Def a bug. Rails doesn't like when you try to send that stuff through with the keys for the tasks as dom_id's which is a bummer. I got a fix you can see here https://github.com/gorails-screencasts/dynamic-nested-forms-with-turbo-get/commit/adfe726005c89f1c71b81f1ddf63c797bddad80a
I'm gonna look into this more when a have some free time to see what else can be done here bc I feel like there's a better option. Sorry about that but thanks for bringing it up, Willard!
Thanks for the videos! One thought on the beginning part where you change the form to use task.hash
. Why not just use that for all the tasks? If you include the ID as a hidden field, which I think you do in your code, I think Rails looks for the ID in the params hash anyways when updating existing records, right?
Hey Collin!
First of all I must say I enjoy your videos very much and appreciate the effort you put into them
From what I can see, the formmethod attribute in the button tag does not accept delete as an option (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#formmethod)
Is rails doing something to override that maybe?
Nice lessen, Currently I'm trying to create a double nested form like this Project -> task -> note using this technique.
But the issue that I run in to now is that when adding a note when creating a new project and task there is no task.id or task object id in the form field for the notes partial to create the correct name <%= fields_for project[task_attributes][task id][note_attributes][note id]
when using 0 for task id it creates a new task for every note you try to add
What would be a clean solution to get the task id or object id in the notes partial?
Managed to get something working by putting the fields_for record_name attribute "project[tasks_attributes][#{task.persisted? ? task.id : task.object_id }]" in a variable and then pass that variable as an parameter to notes_field in the controller. The data is then passed back to the turbo_stream partial notes_field using a global variable, next in the notes partial you can then concatenate the [notes_attributes][#{note.persisted? ? note.id : note.object_id }] to the variable and use that in the new field_for.
Hope that makes any sense