Stef Coetzee

We previously looked at refactoring Rails views with ViewComponent templates. Each template requires a Ruby file and an ERb file. What if there was a way to get rid of the latter?

Since we already have a refactored example application, we get to skip right to swopping out ViewComponent templates for call methods.

As per the docs, we add the Rux Rails gem to the development group of our Gemfile.

# Gemfile
...
group :development do
  gem "rux-rails", "~> 1.0"
end
...

Previously, PageHeaderComponent was defined across two files:

# app/components/page_header_component.rb

class PageHeaderComponent < ApplicationComponent
end

# app/components/page_header_component.html.erb

<div class="pb-2 border-b">
  <%= content %>
</div>

We can get rid of the ERb template by adding a template method to our component class:

Thanks for reading!

#Rails #ViewComponent #Rux

Published:

photo of post author
Stef Coetzee works as a control and automation engineer in Johannesburg, South Africa. He is an avid full-stack Ruby on Rails web developer and technological progress enthusiast.

Stay in touch
Get new posts in your inbox. Unsubscribe anytime.