For example, we have Paper and EvidenceItem models. We want to let the user add/remove/edit EvidenceItems while they are adding or editing Paper records.
The Paper model looks like this:
class Paper < ApplicationRecord
has_many :evidence_items, dependent: :destroy
accepts_nested_attributes_for :evidence_items, allow_destroy: true # add this to allow form nesting
...
The paper controller will need to permit the nested attributes:
def paper_params
params.require(:paper).permit(
:name, :description, :author,
evidence_items_attributes: [ :id, :name, :summary, :link, :source_type, :_destroy]
)
end
Then we use the above stimulus controller and form fields to manage the nested resource.