To render outside of the usual request cycle or view context, e.g. in a model or a background job:
ApplicationController.render partial: 'cat'
To render a ViewComponent
(renders the component including layout):
ApplicationController.render(
CatComponent.new(cat: Cat.last)
)
To render a ViewComponent
without a layout (useful when using TurboStream):
ApplicationController.render(
CatComponent.new(cat: Cat.last),
layout: false
)
To render with request parameters, useful when the controller defines default_url_options
which are not available outside the request cycle:
ApplicationController
.renderer
.with_defaults(
'action_dispatch.request.parameters' => { 'cat_id' => '13' }
)
.render(CatComponent.new(cat: Cat.last))