Accept_nested_attributes_for: allow_destroy ,: _destroy not working

I have a Rails 4.1 application that uses several well-known technologies.

Simple form, Cocoon

I'm having trouble deleting records that are nested attributes. Based on some lengthy research, I believe that my code is correct, however, maybe I am missing something stupid.

Model

has_many :staff_services_joins, :dependent => :destroy has_many :services, :through => :staff_services_joins accepts_nested_attributes_for :staff_services_joins, :allow_destroy => true 

This is a little unorthodox, but I have two components on the join model that are not foreign keys and need to be installed. This is why I accept nested attributes for the join model, and not for the service model.

Controller method

 def update ap staff_params # if @staff_member.update_with_account staff_params, params[:password] if @staff_member.update_attributes staff_params flash[:notice] = 'Successfully updated staff member! :)' redirect_to vendor_store_staff_path current_store, @staff_member else flash[:error] = 'Failed to update staff member :(' render :edit end end 

Strong parameters

 params.require(:staff).permit( :user_id, :store_id, :avatar, :remote_avatar_url, :first_name, :last_name, :email, :active, :admin, :staff_services_joins_attributes => [ :staff_id, :service_id, :price, :duration, :_destroy ] ) 

Hash Parameter Update Example

 { "store_id" => "2", "avatar" => "avatar.png", "remote_avatar_url" => "", "first_name" => "Joshua", "last_name" => "Tyree", "email" => " joshuat@createthebridge.com ", "active" => "0", "admin" => "1", "staff_services_joins_attributes" => { "0" => { "service_id" => "2", "price" => "50.00", "duration" => "30", "_destroy" => "1" } } 

}

Based on this, this thing should destroy this model, but for some reason this, of course, is not so. Any help is most definitely appreciated.

+3
ruby ruby-on-rails activerecord ruby-on-rails-4 simple-form
source share
1 answer

To use accepts_nested_attributes_for with Strong options , you need to specify which nested attributes the whitelist should be. You did this, but you missed adding :id , which is required to complete the delete operation. The "_destroy" key marked the entry to be deleted, but in order to find the entry and delete internally, it must have :id .

You can read the Object Removal Guide .

+6
source share

All Articles