Rails deletes record on edit and not submit - ruby-on-rails

Having the following associations:
Workout has_many workout_sets through ...
workout_set has_many workout_steps through ...
When editing an object I'm experiencing the following issue:
1) go to /model/:id/edit
2) checking the db, once the page loads the top level attributes (the non-nested ones) are deleted from the db once the page loads, but the select marks as selected the correct values, as if in the moment of the load the data was correct.
3) leaving the page without saving (returning to /model/:id) does the following:
3.1) don't update the object, due to no form is submitted.
3.2) leave me with a model without it's primary properties, while the deepest nested attributes remain unchanged.
I'm using cocoon and simple_form to handle nested models. Is it something on Rails I'm missing out?
P.S: I can provide code if needed.

For the record:
The solution is as simple as:
:force_non_association_create => true on each link_to_add_association. For more information check this

Related

Rails 6 nested form cocoon won't save sub-objects

I am trying to implement a nested form for a Book object which can have several BookAuthor objects. I have followed this tutorial to do this with the Cocoon gem in Rails 6, however it does not work. The form itself works well on the front end, adding and removing the BookAuthor fields properly. The params that the server receives contain the correct number of BookAuthor attributes but these sub-objects do not get written to the database, even though the main Book object does. I have been able to get the association to work properly.
There is no error output from either the javascript or rails consoles.
Notes: Ignore the include Hashid::Rails in book.rb, I have tried both with and without this and the same thing occurs. Also, ignore any references to Chapter objects in book.rb, as these are not part of the form yet.
Below is the code for the relevant models, views and controllers. The model files also contain the table schema for that model. If there is any more code you require, please let me know.
Params the server receives (from the server console): https://pastebin.com/nxAXkD3T
book.rb: https://pastebin.com/Xtxf52eT
book_author.rb: https://pastebin.com/miFhb5wR
_form.html.haml: https://pastebin.com/YaW1gRUe
_book_author_fields.haml: https://pastebin.com/FJw7CR2f
books_controller.rb: https://pastebin.com/0NYsCc7x
application.js: https://pastebin.com/rZLpe0iS
Gemfile: https://pastebin.com/TcGrQ9f5
Gemfile.lock: https://pastebin.com/apCMxNkJ
package.json: https://pastebin.com/9U7mB2NK
yarn.lock: https://pastebin.com/H3AzG3Bi
There is no need to add the attr_accessor :book_authors_attributes. in your book.rb. remove it.
This is automatically added with some rails magic.

Active Admin: Add Entries to report

So what I have is a report model and an entries model. The report has many entries and entries belong to the report. What I am wanting to do is be able to click a button on the show page of the report and add new entries. Is there a way of doing this? A non-ajax solution is fine.
Cocoon is an excellent gem for this.
It gives you a Remove button for each entry and an Add button to add as many entries as you want. Add accepts_nested_attributes_for :entries to your Report model. You need to put the entries form in their own partial but that's not a hardship, and cocoon automatically creates the show/hide jquery for you. Be sure to include and whitelist the entries' id column as a hidden field otherwise you can end up with duplicate entries, and you also need to whitelist _delete (that's underscore + delete) so that the entries can be deleted by rails when required.

Active Admin bogged down with relationship; how to customize?

I've got a Product class that has_many Events -- in most cases there can be thousands of saved events -- and I've no need to display these on the ActiveAdmin Product page but ActiveAdmin is trying to load them anyway, which makes the app crash.
How can I best have the Product admin page ignore the relationship? Something to do with customizing the collection? Putting an empty scope on the Event model and calling that as default on the admin page? Really unsure how to fix this.
See the answer here to either remove the filter from the index or scope the filter to a subset of options:
ActiveAdmin automatically loading full association table

Preventing duplicate model objects with nested forms in rails 3

I have a form set up for a Recipes model that accepts_nested_attributes_for Ingredients. The form and saving works fine, however, I want to check for the existence of each Ingredient in the database. The form seems to be creating new records for ingredients, even if they already exist in the database.
Recipes and Ingredients are related using a 'has_many :through' relationship. Is there a clean way to make sure that ingredients are duplicated when the form is submitted, but the relationships are still established?
similar question might help you on this, although the solution is not that eligant
Rails nested form on many-to-many: how to prevent duplicates?
It is about how you are taking input from the user - your forms are.
If you have a multiple select input for ingredients in new/edit form of recipe and you show all the existing ingredients there, user can choose the existing ingredients and the POST/PUT from these forms will have the existing ingredients' ids as part of form data. This will not create new ingredients then.

Rails Dynamic Form Fields & AJAX Submit

I have a Ruby on Rails app that has a form with nested attributes. This form allows the dynamic addition of fields with some javascript. The form also submits itself on blur via AJAX.
Everything is working great, with the exception of one issue that centres around the dynamic addition of fields.
When the dynamic fields are added via Javascript, they are assigned a unique ID. However, the ID and pattern of names for form fields obviously does not match what rails would generate once the fields have been saved.
Because of the AJAX form submit, the dynamically added fields create a new DB entry every time the form is changed.
I need to find a way to gracefully handle the fact that I need to retrieve the rails generated form and modify/replace the one on the page after an AJAX submit - otherwise new fields will continue to replicate!
You can see my code at: https://github.com/michaelward82/WeddingPlanner/tree/UI
(Apologies if my Rails code is not the cleanest, this is my first genuine attempt at a real rails project and I'm still learning the patterns)
I think what you're missing is a :dependent => :delete_all parameter on the belongs_to association. That makes Rails delete the old budget items when a new set is saved:
# budget_item.rb
belongs_to :budget, :dependent => :delete_all

Resources