Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoon how to remove associations

I am trying to use the Cocoon gem to add/remove assets on asset_profiles. Everything up to this point works, I am just having a problem with link_to_remove_association. When the button is clicked, the field is removed. But if the field had been filled in and saved previously, I am unable to remove said association. The button just hides the field until I hit update. Is there a way to actually remove the association from the database through link_to_remove_association? Any help is much appreciated. Thanks in advance.

Here is the code I am referring to:

Asset.show

= simple_form_for([@asset_profile, @asset]) do |f|
  = f.input :max_users, as: :hidden
  #assets_users
    = f.simple_fields_for :assets_users do |assets_user|
      = render "assets_user_fields", f: assets_user
    .links
      = link_to_add_association "Add Another User", f, :assets_users
    = f.submit

Asset._assets_users_fields

.nested-fields
  = f.input :user_id, collection: @users.order(:last_name), :label => "User"
  = link_to_remove_association "Remove", f

Screenshot of page pre-remove: pre-remove Screenshot of post-remove: post-remove Screenshot of post-update(page reload): post-update

I would much rather, after the update, the page be reloaded and looking like below, which is the form for initially adding users to an asset: new asset users

like image 998
Briknthewall Avatar asked Dec 05 '25 12:12

Briknthewall


2 Answers

When using strong parameters, you have to make sure to permit :id and :_destroy, as documented by the way (see documentation ).

It is not unlogical: cocoon sets the _destroy if something needs to be removed, rails then needs the id to know what to remove.

like image 167
nathanvda Avatar answered Dec 07 '25 13:12

nathanvda


Im my case, i forget for allow_destroy: true.

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

like image 32
Alexander Vagin Avatar answered Dec 07 '25 11:12

Alexander Vagin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!