简体   繁体   English

accept_nested_attributes_for:allow_destroy,:_ destroy无效

[英]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.

相关问题 accepts_nested_attributes_for:models,allow_destroy:true不起作用 - accepts_nested_attributes_for :models, allow_destroy: true not working accepts_nested_attributes_for allow_destroy不适用于范围验证 - accepts_nested_attributes_for allow_destroy doesn't work with scoped validation 为什么accepts_nested_attributes_for allow_destroy不起作用? - Why doesn't accepts_nested_attributes_for allow_destroy not work? Rails:allow_destroy => true在Active Record嵌套属性中有效,但在开发中有效 - Rails :allow_destroy => true in Active Record Nested Attributes works in development but not production Ruby on Rails - 当 allow_destroy: true 时,nested_attributes 验证出现问题 - Ruby on Rails - Problem with nested_attributes validation when allow_destroy: true 使用accept_nested_attributes_for进行验证 - Validations with accept_nested_attributes_for 如何在Rails中定义allow_destroy和:dependent =>:destroy? - How to define allow_destroy and :dependent => :destroy in Rails? Rails验证最小编号 嵌套形式和allow_destroy时关联对象的数量:true - Rails validate minimum no. of associated object in case of nested form and allow_destroy: true 如何将allow_destroy选项合并到Rails中的自定义嵌套属性编写器中 - How do I incorporate allow_destroy option into a custom nested attribute writer in Rails Rails:销毁不适用于嵌套属性 - Rails: Destroy not working for nested attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM