accept_nested_attributes_for:allow_destroy,:_ destroy无效

我有一个Rails 4.1应用程序,它使用了一些值得注意的技术。

简单的forms,茧

我无法销毁嵌套属性的记录。 基于一些冗长的研究,我相信我的代码是正确的,但我可能会遗漏一些愚蠢的东西。

模型

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

这有点不正统,但我在连接模型上有两个组件,它们不是外键,需要设置。 这就是为什么我接受连接模型的嵌套属性而不是服务模型。

控制器方法

 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 

参数很强

 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

 { "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" } } 

}

基于has,这个东西应该是破坏那个模型,但由于某种原因它肯定不是。 绝对赞赏任何帮助。

要将accepts_nested_attributes_for强参数一起使用,您需要指定哪些嵌套属性应列入白名单 。 你做到了,但你错过了添加:id执行删除操作所需的:id"_destroy"键标记了删除记录,但要在内部找到记录并删除,它需要:id存在于那里。

您可以阅读 删除对象指南。