Polymorphic Parent Resources

Serve a child resource that hangs off many different parents — comments, reports, duplications, attachments — from a single Rails controller, using a route concern plus `resource_for` from the rolemodel_rails gem. Use when a child resource needs to attach to several parent models, when about to write a second or third namespaced controller that differs only by its parent (`Estimates::CommentsController`, `Widgets::CommentsController`), when a route concern passes a `*_type` route default such as `commentable_type` or `reportable_type`, when a controller looks up its parent with a hard-coded `Parent.find(params[:parent_id])` that now needs to support more parents, or when consolidating duplicated per-parent controllers.

rolemodel 68b0a33 2 files · 14.0 KB Updated

File contents

RoleModel/rolemodel-skills/tree/main/skills/polymorphic-parent-resources commit 68b0a331df

Frequently asked questions

npx skillmds@latest add rolemodel/polymorphic-parent-resources