8

I have the same issue as this one

But that solution didn't help me. Here are my strong params:

def request_params
   params.require(:request).permit(:name, :address, :phone, :mobile, :type,
     :filled_cartridges_attributes => [:cartridge_name,:client_id,,
    :count,:_destroy,:id],       

So i have :_destroy and :id. The guy in the previous forgot to add :id to strong params. Adding an id solved his problem.

Here is my request model:

has_many :filled_cartridges, inverse_of: :request, dependent: :destroy
 accepts_nested_attributes_for :filled_cartridges, :reject_if => :all_blank, allow_destroy: true

And this is how params look like after submitting:

request: 
...some params....
filled_cartridges_attributes: !ruby/hash:ActionController::Parameters
    '0': !ruby/hash:ActionController::Parameters
      cartridge_name: HP LaserJet3000
      _destroy: 'false'
      id: '1'
    '2': !ruby/hash:ActionController::Parameters
     cartridge_name: new 9
     _destroy: '1'
     id: '13'

Here the 2nd one should be destroyed. But it doesn't.

Community
  • 1
  • 1
yerassyl
  • 2,958
  • 6
  • 42
  • 68
  • How do you assign params to the model? – BroiSatse Apr 23 '15 at 08:50
  • @BroiSatse what do you mean? – yerassyl Apr 23 '15 at 08:52
  • You got your params in your controller, but what do you do with them? Could you show the controller action? Also, is the first object updated? – BroiSatse Apr 23 '15 at 08:53
  • @BroiSatse, My controller method has too much of code not related to the destroy. In update action i update the fields, and it works, but there is no code for deleting, as I understood it should delete automatically due to _destroy param. The gem cocoon doesn't tell anything about it. In railscasts tutorials lecturer said that it destroys automatically if _destroy is provided. Am i right? – yerassyl Apr 23 '15 at 08:57
  • Yes, you're correct, providing you are using `filled_cartridges_attributes=` method (which is internally used by `assign_params` and other methods). This will only mark associated record for destruction, it will not destroy model until you call `save` on it, so the way you assign your params is important. – BroiSatse Apr 23 '15 at 09:02
  • @BroiSatse, yep i have .save in my method. Also i noticed one thing. Records that are marked to be deleted change their id's in DB. ex: 19,20,21 ids changed to 22,23,24 ids. I mean 3 records marked to destroy. – yerassyl Apr 23 '15 at 09:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76023/discussion-between-broisatse-and-user3565829). – BroiSatse Apr 23 '15 at 09:04

1 Answers1

0

You should add :_destroy and :id on your permitted parameters to the specified nested attribute.

Hamdan
  • 183
  • 15