Homepage

Ruby on Rails View Components with Trailblazer Cells

JavaScript is now more composable than ever, and it feels it won’t change for a long time as it’s one of the most efficient ways of creating websites and web apps now. But how do Ruby on Rails and View Components cooperate with each other? Is it possible to include component-based structure within RoR applications? With Trailblazer Cells, it is.

In recent times view components became a really popular web design pattern. View component is standalone part of view, which can be used at many views.

Most common JavaScript frontend frameworks like React, Ember or Angular use view components pattern. Even last hot topic in web world "Web Components" is just implementation of view components.

Ok, but it’s all about JavaScript world. What if we want to stay in Rails world? Can we use view components in RoR app? Yes, We can! We can do it with Trailblazer Cells.

On the first page of Cells documentation we read:

A cell is an object that represent a fragment of your UI. The scope of that fragment is up to you: it can embrace an entire page, a single comment container in a thread or just an avatar image link.

I could just list adventages of cells, but you can find them in the documentation. I would like to show simple example of cells’ usage. I hope it will show you benefits of view components in rails app.

The example is a simple app with CRUD actions for below database.

Let’s start by creating the app with scaffolds.

  1. rails new cells-app

  2. cd cells-app

  3. rails g scaffold company name:string city:string

  4. rails g scaffold department name:string company:references

  5. rails g scaffold employee first_name:string last_name:string department:references

  6. rake db:create db:migrate

  7. add root to: ‘companies#index’ to config/routes.rb

We’ve created base app with scaffolds for company, department and employee. Now we can configure cells’:

  1. Add to gemfile:

    • gem "trailblazer-cells"

    • gem "cells-erb"

    • gem "cells-rails"

  2. Run bundle install

  3. Run mkdir app/cells

So, finally we can go to most interesting part – cells implementation. If we list the app/views we will see that we already have a lot of views:

We will use code from these views in our cells.

We will create cells for each type of view:

  • IndexCell

  • ShowCell

  • NewCell

  • EditCell

And some view components cells:

  • TableCell

  • TitleCell

  • FormCell

  • NoticeCell

We will replace each template with cell. Completed project you can find on GitHub. Here I will just show two examples of cells: TableCell and IndexCell.

TableCell

Firstly, we need to create files structure. TableCell is built from one ruby file and four erb(html) files. Our cells’ directory should look like below.

table_cell.rb is the core of our cell, it contains all helpful methods.

table_cell.rb

tsx

The main view part of TableCell is show.erb, it will be rendered while we will use the cell. It contains table tag and renders header.erb and body.erb views.

table/show.erb

tsx

In header.erb we put thead tag and th tag for each attribute provided to cell.

table/header.erb

tsx

In body.erb we render table row for each item provided to cell.

table/body.erb

tsx

row.erb creates td tag for each attribute of the item.

table/row.erb

tsx

IndexCell

At first we need to add abstract PageCell with html layout, the main job of layout is including javascript and stylesheets assets. Each view needs to load the layout, so views cells will inherit from the PageCell. We have to create app/cells/page_cell.rb and app/cells/page/layout.erb files.

page_cell.erb

tsx

page/layout.erb

tsx

Let’s speed up. I’ve already created TitleCell and NoticeCell. We need them for IndexCell. IndexCell displays all of needed components on index page.

index_cell.erb

tsx

index/show.erb

tsx

Implementation of others cells you can see at the repository. So, we have all cells ready. Let’s use them.

companies_controller.rb

tsx

departments_controller.rb

tsx

employees_controller.rb

tsx

As you can see we have used the same cells for all controllers. That is the power of cells. Now If you want to change title header size for all pages, you have to just change one file, the TitleCell:

tsx

That’s all. The change at one file is propagated to all views. How cool is that? And that is the sense of cells.

Of course, cells have more advantages, but I had only one goal in this article. I wanted to show you that with cells you can create flexible and reusable views at rails. I hope that now you can see it.

Let’s Create a Great Website Together

We'll shape your web platform the way you win it!

More posts in this category

  • February 05, 2025 • 10 min read

    API-first CMS: What Options You Have (Web Dev Agency's Take)

    According to the 2024 State of the API Report by Postman, 74% of organizations are adopting API-first strategies. This statistic isn’t just impressive—it signals a major shift in how businesses operate. While API-first approaches have long been a staple in software development, they're now reshaping content management as well. More and more companies are realizing that traditional CMS platforms can't keep up with the demand for flexibility, speed, and seamless integrations.

    READ MORELearn more about api first cms
  • January 23, 2025 • 15 min read

    Best CMS for SaaS: Top Cloud-Based Solutions

    Choosing the right Content Management System (CMS) is a critical decision for your SaaS business. Your unique needs require solutions that are not only flexible, scalable, and user-friendly but also tailored to meet the demands of a fast-paced, customer-focused industry. A CMS should simplify your workflows and help you deliver personalized, high-quality digital experiences.

    READ MORELearn more about best cms for saas top cloud based solutions
  • December 12, 2024 • 10 min read

    We Picked the Best (Headless) CMS for Vue

    Picture a digital experience where content effortlessly flows across platforms, development is agile, and performance is unmatched. By combining the power of Vue.js, a progressive JavaScript framework, with a modern headless CMS, you can achieve all this and more.

    READ MORELearn more about headless cms for vue