-1

I'm currently using cocoon rails gem to create nested forms.

The problem happens when I edit the "father" entity and remove some nested items, it adds another one instead of removing it. I created a gist file that explains it better with ruby on rails console logs. Here it is: https://gist.github.com/msmosso/310835bbb1a4b7cfe1a9.

Thanks for advance. Cheers!

Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
msmosso
  • 15
  • 5
  • Please show us some code. I would especially need the view and controller code to be able to say something sensible. – nathanvda Aug 28 '14 at 19:32
  • Here comes the code: https://gist.github.com/msmosso/6c48fb2cd0117ad43f31. It contains the form, the nested fields partial, clients controller and client and store models. – msmosso Aug 29 '14 at 02:30
  • That's all your code? I see no immediate mistake or explanation for the behaviour you describe. Can you make a minimal rails project with this error and put it on github and send me the link? The code as shown should just work imho. – nathanvda Aug 29 '14 at 12:55
  • For sure. The page in question is /clientes/novo: https://github.com/msmosso/cocoon – msmosso Aug 29 '14 at 15:54

2 Answers2

0

Does the answer to this question help?

Cannot destroy existing nested association

Specifically the part about allow_destroy. Deleting through the update action seems fairly irregular. Perhaps something is wired up incorrectly.

Community
  • 1
  • 1
Michael Pell
  • 1,416
  • 1
  • 14
  • 17
  • I already have allow_destroy, here comes my Client model: https://gist.github.com/msmosso/8a69e582ecdeb6813058 – msmosso Aug 21 '14 at 20:24
  • I also allowed _destroy as you can see here: https://gist.github.com/msmosso/1b42a62d1e1f21843cc7 – msmosso Aug 21 '14 at 20:26
0

In your client_params you specify which parameters are allowed for a client, and a nested store. You did not specify the :id for a store, and that makes sure that each time a client is saved, it will be stored as a new one. So the fix is actually pretty simple:

def client_params
  params.require(:client).permit(:name, stores_attributes: [:id, :client_id, :name, :name2, :district, :address, :number, :complement, :cnpj, :city_id, :mail, :obs, :status, :phone, :phone2, :phone3, :sversion, :_destroy])
end

I did not see that until I tried it, doh!

So: I added :id and removed :created_at and :updated_at (the last two are not really relevant, but you do not want those in that list).

nathanvda
  • 49,707
  • 13
  • 117
  • 139