Skip to content

Scaffolding nested resources in console

Petr Marek edited this page Jan 31, 2022 · 1 revision

Say you have a Product which has_many :variants. You might want to administer the variants at /console/project_namespace/products/:product_slug/variants. To do so, generate the Variant console scaffold with the --through option:

rails g folio:console:scaffold ProjectNamespace::Variant --through ProjectNamespace::Product

This sets the correct directory structure and changes the folio_console_controller_for like so:

folio_console_controller_for "ProjectNamespace::Variant", through: "ProjectNamespace::Product"

Which makes the folio_console_controller_for run load_and_authorize_resource for the parent Product record and then load_and_authorize_resource for Variant with the through: :product option. To access the parent record in the controller, use the default-named cancancan instance variable (i.e. @product).