Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deletion of nested attributes does not work

I seem to be unable to delete items using the accepts_nested_attributes_for command but I have done this according to this tutorial and the associated git repo. My models are ...

class Job < ActiveRecord::Base
    has_many :specialties, :inverse_of => :job
    accepts_nested_attributes_for :specialties, allow_destroy: true, :reject_if => :all_blank
end

class Specialty < ActiveRecord::Base
    belongs_to :job, :inverse_of => :specialties
end

In my Job form, I have...

<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Specialty" %>

When I click the checkbox to delete the a couple of specialties, nothing happens. I checked the server output and received:

Started PATCH "/jobs/1" for 127.0.0.1 at 2013-07-16 16:15:16 -0400

Processing by JobsController#update as HTML

Parameters: {"utf8"=>"✓", "authenticity_token"=>"8VxYXujcKXpLEm8+7B43SLU6X3fH00kIOmFK+nvaBKs=", "job"=>{"name"=>"Cook", "description"=>"Makes the food.", "specialties_attributes"=>{"2"=>{"name"=>"", "description"=>"", "_destroy"=>"1", "id"=>"3"}, "3"=>{"name"=>"", "description"=>"", "_destroy"=>"1", "id"=>"4"}}}, "commit"=>"Update Job", "id"=>"1"}

Job Load (0.1ms) SELECT "jobs".* FROM "jobs" WHERE "jobs"."id" = ? LIMIT 1 [["id", "1"]]

Unpermitted parameters: _destroy

Unpermitted parameters: _destroy

Unpermitted parameters: _destroy

Unpermitted parameters: _destroy

What did I miss? I've gone through the tutorial and repo a bunch of times and I can't see where I've gone off.

like image 662
Jeff Avatar asked Nov 27 '22 17:11

Jeff


1 Answers

That's because of strong_parameters. You now have to permit keys. In your action:

params.permit(:_destroy)

like image 88
Damien Roche Avatar answered Dec 07 '22 21:12

Damien Roche