accept_nested_attributes_for:allow_destroy,:_destroy不工作 [英] accept_nested_attributes_for :allow_destroy, :_destroy not working
问题描述
我是用几个显着的技术,一个Rails 4.1应用程序。
I have a Rails 4.1 application which is using a few notable technologies.
简单的形式,茧
我在其中嵌套属性的麻烦销毁记录。基于一些长期的研究,我相信我的code是正确的,但我可能会丢失一些愚蠢的。
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.
型号
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.
控制器方法
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
]
)
例如,更新PARAMS散列
Example update params 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"
}
}
}
根据对了,这个东西应该被销毁的模型,但由于某些原因,它肯定是不。任何帮助是最肯定的AP preciated。
Based on the has, this thing should be destroying that model, but for some reason it certainly isn't. Any help is most definitely appreciated.
推荐答案
为了使用 accepts_nested_attributes_for
是强大的参数,您需要指定嵌套的属性应该是白名单。你做到了,但是你错过了添加:ID
这是需要执行的删除操作。 _消灭
键标记为删除的记录,但找到的记录进行删除的内部的,它需要有 :ID
present有
In order to use accepts_nested_attributes_for
with Strong Parameters, you will need to specify which nested attributes should be whitelisted. You did it, but you missed to add :id
which is needed to perform the deletion operation. "_destroy"
key marked the record for deletion, but to find the record out and delete internally, it needs to have :id
present there.
您可以的读的删除对象指南。
这篇关于accept_nested_attributes_for:allow_destroy,:_destroy不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!