Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Mongoid _destroy is not deleted embedded document using nested_form gem #117

Open
@jerryshen

Description

@jerryshen

I am on Rails 3.1, Mongoid 2.3.3, and using the nested_form gem. In my form, I have the nested_form link_to_add and link_to_remove set up to add and remove an embedded document in my model. The link_to_add helper method works great, but the link_to_remove help method changes are not persisted in MongoDB. In the rails output, I can see the JSON parameter passed to Mongoid has the _destroy: 1 value set but the change is not saved to MongoDB.

Here is the model

class Product
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name,            :type => String

  embeds_many :photos, :as => :photogenic, :cascade_callbacks => true

  accepts_nested_attributes_for :photos,
                            :reject_if => proc { |attributes| attributes['data'].blank? },
                            :allow_destroy => true


end

Here is the params:

"photos_attributes"=>{"0"=>{"_destroy"=>"1", "data_cache"=>"", "id"=>"4ec48e8e6985605b5d000073"}, "1"=>{"_destroy"=>"1", "data_cache"=>"", "id"=>"4ec48e8e6985605b5d000074"}, "2"=>{"_destroy"=>"false", "data_cache"=>"", "id"=>"4ec4925a6985605b5d000083"}, "3"=>{"data_cache"=>"", "_destroy"=>"false"}

I'm sure I missed something, but I haven't been able to figure out why the embedded document isn't removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions