---
title: Rendering view component template variants outside of the current request
author: Daniel Schoppmann
tags: Rails, View Components
published_at: '2024-02-22T16:03:00+01:00'
updated_at: '2024-07-18T09:04:28+02:00'
canonical_url: https://www.dotruby.com/articles/rendering-variant-templates-of-view-components-outside-of-the-current-request
---

# Rendering view component template variants outside of the current request

Discover how to leverage ActionController::Rendering for rendering specific component variants outside the current view context with ease.

Rails variants are a technique to allow rendering of different template or partial variants, for example, to render different web and ios/android views, even though the underlying controller logic is the same. If you'd have a partial `_new.html.erb`, you'd could also provide a partial `_new.html+ios.erb` which would automatically being called if the request object is set to this preferred variant.  

As heavy users of GitHub's `view_component` gem, we are happy that the gem supports variants natively as well. However, sometimes we run into a situation where we want to access a variant of the component without relying on the current request variant. It turns out that calling the component with the usual `render MyComponent.new, variant: 'custom_variant'` does not work. It's tight to the current view context of the given controller.

Luckily there is the Rails `ActionController::Rendering` class, which is exactly made for this: To [render arbitrary templates without being inside a controller action](https://api.rubyonrails.org/classes/ActionController/Renderer.html). 

So, for example, if you are in a place in your Rails view and you need to explicitly address a variant template that is outside the current view_context variant, you can do so with:

```ruby
<%= ApplicationController.render(MyComponent.new, variant: 'custom_variant') %>
```
