Giving Ruby objects access to Rails view methods

I think most Rubyists can agree that POROs (Plain Old Ruby Objects) are incredibly useful. When done right, they’re easy to understand, build, and use in applications.

In the Rails ecosystem, there are many classifications of POROs: services, presenters, query objects, value objects, and so on and so forth. Sometimes we want to give these POROs access to things our application can access at the view level—maybe we want a PORO to manage generating a set of asset or route paths for us.

Luckily, it’s easy! We can use something called view_context.

class MyController < ApplicationController
  def index
    # Pass a `view_context` from the controller level into a PORO.
    poro =

    # ...

class MyPoro
  def initialize(context)
    @context = context

  # Now we have access to things like...
  def example
    # Image paths.

    # Routes.

    # Other things you have access to in a view.
    # etc...

Having the flexibility to access view-level methods in a PORO is powerful for obvious reasons and can help prevent unnecessary complexity inside of templates and partials.