nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 385 forks source link

Unable to detect error in saving a nested object with cocoon (302 message) #564

Closed Tumble25 closed 5 years ago

Tumble25 commented 5 years ago

Dear All,

First of all, awesome gem! I worked with cocoon before and it always worked like a charm.

Currently I am working with the gem again in a new project, but I am now encountering a problem with saving. I summarised the problem incl. code/error message on StackOverflow some time ago, but unfortunately this didn't help. Please find the link below.

https://stackoverflow.com/questions/57842748/unable-to-detect-error-in-saving-a-nested-object

Please let me know if you need more information or have me write down my code + 302 message on this platform as well.

nathanvda commented 5 years ago

I added an answer there. It seems you have a problem with failing validations.

Tumble25 commented 5 years ago

You're great and thanks for the swift response! I already responded on StackOverflow, but I will repeat my findings so far (so hopefully others can benefit from the current debugging process as well).

The problem seems to be that my program doesn't recognise the relationship between both tables properly.

I tried debugging with simple_form, but surprisingly, there are no error messages. However following your reasoning, I tried getting rid of my blank? validating in the model and subsequently creating new (nested) options via the controller --> 3.times {@accommodation_category.options.build} I do get an error message saying "ActionController::ParameterMissing in AccommodationCategoriesController#update" AND "param is missing or the value is empty: accommodation_category" .

Tumble25 commented 5 years ago

I have found the error and @nathanvda was spot on, the params were not correct causing a rollback. I though it might be useful for others to describe how I found out for nested tables, using the cocoon gem.

1) I went to my console and tried creating a new accommodation_category with options, which caused the rollback.

cat11=AccommodationCategory.create(name:'hello', options_attributes:[{name:'option 1'}])

2) Then I checked if it was valid (obviously not)

cat11.valid?

3) Lastly, I displayed the error messages

cat11.errors.full_messages
=> ["Park must exist", "Options age table must exist", "Options price must exist"]

I'm pretty sure I did something wrong with the debugging @nathanvda mentioned, which should have given me the same error message. I fixed the errors by linking the price & age_table (which I should have posted in my question in the first place, but didn't because I deemed it not relevant).