From 589ec84637d0c03b8ea330e725509da058ab5113 Mon Sep 17 00:00:00 2001 From: maebeale Date: Sat, 7 Mar 2026 06:53:38 -0500 Subject: [PATCH 1/2] Allow comment deletion via nested attributes Enable allow_destroy on accepts_nested_attributes_for :comments across all commentable models (Person, User, Organization, EventRegistration) and permit :_destroy in their controllers. Adds a "Remove" link to the comment fields partial. Co-Authored-By: Claude Opus 4.6 --- app/controllers/organizations_controller.rb | 2 +- app/controllers/people_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- app/models/event_registration.rb | 2 +- app/models/organization.rb | 2 +- app/models/person.rb | 2 +- app/models/user.rb | 2 +- app/views/comments/_comment_fields.html.erb | 2 ++ 8 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 7ed3c28b8..23f09e6d5 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -199,7 +199,7 @@ def organization_params :end_date, :_destroy ], - comments_attributes: [ :id, :body ], + comments_attributes: [ :id, :body, :_destroy ], addresses_attributes: [ :id, :address_type, diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index b8d7128bd..3e72a4e78 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -399,7 +399,7 @@ def person_params :end_date, :_destroy ], - comments_attributes: [ :id, :body ], + comments_attributes: [ :id, :body, :_destroy ], ) end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 966402b7f..6cc67afe7 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -408,7 +408,7 @@ def user_params :phone, :phone2, :phone3, :birthday, :best_time_to_call, :notes, # legacy to remove later ##### - comments_attributes: [ :id, :body ], + comments_attributes: [ :id, :body, :_destroy ], affiliations_attributes: [ :id, :organization_id, :position, :title, :inactive, :primary_contact, :start_date, :end_date, :_destroy ], ) end diff --git a/app/models/event_registration.rb b/app/models/event_registration.rb index 793c196a5..92dd5ca2a 100644 --- a/app/models/event_registration.rb +++ b/app/models/event_registration.rb @@ -9,7 +9,7 @@ class EventRegistration < ApplicationRecord before_destroy :create_refund_payments - accepts_nested_attributes_for :comments, reject_if: proc { |attrs| attrs["body"].blank? } + accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attrs| attrs["body"].blank? } before_create :generate_slug after_create :snapshot_registrant_organizations diff --git a/app/models/organization.rb b/app/models/organization.rb index 11fdf835e..e9c0ee9ae 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -48,7 +48,7 @@ class Organization < ApplicationRecord after_save :remove_duplicate_sectorable_items accepts_nested_attributes_for :affiliations, allow_destroy: true, reject_if: proc { |attrs| attrs["person_id"].blank? } - accepts_nested_attributes_for :comments, reject_if: proc { |attrs| attrs["body"].blank? } + accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attrs| attrs["body"].blank? } # SearchCop include SearchCop diff --git a/app/models/person.rb b/app/models/person.rb index 304bbd5c5..556994930 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -62,7 +62,7 @@ class Person < ApplicationRecord accepts_nested_attributes_for :user, update_only: true accepts_nested_attributes_for :affiliations, allow_destroy: true, reject_if: proc { |attrs| attrs["organization_id"].blank? } - accepts_nested_attributes_for :comments, reject_if: proc { |attrs| attrs["body"].blank? } + accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attrs| attrs["body"].blank? } # Search Cop include SearchCop diff --git a/app/models/user.rb b/app/models/user.rb index 536d7b072..b9d6ad8ff 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,7 +55,7 @@ class User < ApplicationRecord has_many :user_form_form_fields, through: :user_forms, dependent: :destroy # Nested attributes accepts_nested_attributes_for :user_forms - accepts_nested_attributes_for :comments, reject_if: proc { |attrs| attrs["body"].blank? } + accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attrs| attrs["body"].blank? } before_validation :strip_whitespace diff --git a/app/views/comments/_comment_fields.html.erb b/app/views/comments/_comment_fields.html.erb index fbf81b8b3..d749f4a10 100644 --- a/app/views/comments/_comment_fields.html.erb +++ b/app/views/comments/_comment_fields.html.erb @@ -50,6 +50,8 @@ <%= truncate(f.object.body, length: 135) %> + <%= link_to_remove_association "Remove", f, + class: "text-sm text-gray-400 hover:text-red-600 underline whitespace-nowrap" %> + <%= link_to_remove_association "Remove", f, + class: "text-sm text-gray-400 hover:text-red-600 underline whitespace-nowrap mt-2 shrink-0" %> <% else %> <% current_first_name = current_user&.person&.first_name || current_user&.name.to_s.split.first %>