[英]accept_nested_attributes_for :allow_destroy, :_destroy not working
I have a Rails 4.1 application which is using a few notable technologies. 我有一个Rails 4.1应用程序,它使用了一些值得注意的技术。
Simple Form, Cocoon 简单形式,茧
I'm having trouble destroying records which are nested attributes. 我无法销毁嵌套属性的记录。 Based on some lengthy research, I believe my code is correct, however I may be missing something silly.
基于一些冗长的研究,我相信我的代码是正确的,但我可能会遗漏一些愚蠢的东西。
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
It's a little unorthodox, but I have two components on the join model which are not foreign keys and need to be set. 这有点不正统,但我在连接模型上有两个组件,它们不是外键,需要设置。 That is why I'm accepting nested attributes for the join model rather than 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
]
)
Example update params hash 示例更新参数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"
}
}
} }
Based on the has, this thing should be destroying that model, but for some reason it certainly isn't. 基于has,这个东西应该是破坏那个模型,但由于某种原因它肯定不是。 Any help is most definitely appreciated.
绝对赞赏任何帮助。
In order to use accepts_nested_attributes_for
with Strong Parameters , you will need to specify which nested attributes should be whitelisted . 要将
accepts_nested_attributes_for
与强参数一起使用,您需要指定哪些嵌套属性应列入白名单 。 You did it, but you missed to add :id
which is needed to perform the deletion operation. 你做到了,但你错过了添加
:id
执行删除操作所需的:id
。 "_destroy"
key marked the record for deletion, but to find the record out and delete internally , it needs to have :id
present there. "_destroy"
键标记了删除记录,但要在内部找到记录并删除,它需要:id
存在于那里。
You can read Removing Objects guide. 您可以阅读 删除对象指南。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.