删除嵌套属性不起作用 [英] Deletion of nested attributes does not work

查看:46
本文介绍了删除嵌套属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法使用 accepts_nested_attributes_for 命令删除项目,但我已根据 本教程 和相关的git repo.我的模型是...

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

在我的 Job 表单中,我有...

In my Job form, I have...

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

当我单击复选框删除几个 specialties 时,没有任何反应.我检查了服务器输出并收到:

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

开始修补/jobs/1";对于 127.0.0.1 在 2013-07-16 16:15:16 -0400

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

由 JobsController#update 处理为 HTML

Processing by JobsController#update as HTML

参数:{utf8"=>✓",authenticity_token"=>8VxYXujcKXpLEm8+7B43SLU6X3fH00kIOmFK+nvaBKs=",名称"=作业">Cook"、description"=>制作食物"、specialties_attributes"=>{2"=>{name"=>", description"=>", _destroy"=>1", id"=>3"}, 3"=>{name"=>"、描述"="、_destroy"=1"、id"=4"}}}、提交";=>更新作业",id"=>1"}

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

作业负载 (0.1ms) SELECT "jobs".* FROM "jobs";WHERE "jobs"."id"= ?限制 1 [[id", 1"]]

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

不允许的参数:_destroy

不允许的参数:_destroy

不允许的参数:_destroy

不允许的参数:_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.

推荐答案

那是因为 strong_parameters.您现在必须permit 键.在你的行动中:

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

params.permit(:_destroy)

这篇关于删除嵌套属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆