The RailsNotes Newsletter 🟥 ISSUE #15

🟥 ISSUE #15 (ViewComponent tips, a brilliant preso, and pairing with Turbo)

It’s ViewComponent time! 👀 Your Rails frontends have never looked so good (and been so easy to work on!)

— SPONSOR —

Free RailsNotes UI update — 4 new ActionMailer email templates → 

I’ve released new email templates for RailsNotes UI! RailsNotes UI is my side-project; It’s a collection of email templates and components built for ActionMailer and Ruby on Rails (and uses ViewComponents under the hood!).

I occasionally release free updates, but this time I’ve gone all-out and release 4 new templates!

Welcome to The RailsNotes Newsletter — Issue #15! This issue is all about ViewComponents!

Hey! 👋 It’s great to be back! I took a break from writing last week, but I’m excited to be back here today with another issue of the RailsNotes newsletter for you all!

This week we’re looking at ViewComponents (again)! Can you tell I love them? 😅 I found extra time to write this week, so I put together a ✨ new blog article ✨ collecting all my tips and tricks for writing ViewComponents.

I’ve also included a few other great ViewComponent articles — a great slide deck from a ViewComponent presentation, a walkthrough of pairing ViewComponents with Turbo, and a deep dive into the problems ViewComponents solve.

I think you’ll love this one! Let’s dive in!

~ FEATURED ARTICLE ~

The smile you’ll have after reading this article and learning some great new ViewComponent tips 😊 

I keep going on and on about it (like in previous newsletters), but I love ViewComponents! They feel like the missing piece of the Rails frontend stack — partials and templates are fine, but I’ve found they become unwieldy pretty quickly. ViewComponents are a great way to better encapsulate your front-end logic.

I’ve used ViewComponents heaps in my projects, including building an entire email template and component library with them. I’ve picked up quite a few tips and tricks along the way, and I finally sat down and turned them all into an article!

This is that article — It’s got 7 handy tips to write better ViewComponents. I cover component namespaces like UI::, aliasing slot helpers, better filenames and more!

If you use ViewComponents in your projects, I think you’ll find this article really handy. And if you’ve got tips of your own that I’ve missed, please send them to me! Just reply to this email.

~ MORE ARTICLES  ~

This is a brilliant set of slides from a talk by @rstankov called “Component-driven UI with ViewComponent gem”. Radoslav goes deep into how he’s used ViewComponents to build AngryBuilding (a facility management SaaS).

This is a great way to study ViewComponent usage in the wild, and pick up some great tips along the way!

This is a short and sweet article about combining ViewComponents with Turbo frame — Jason does a great job explaining how the two technologies work together, as he walks you through building a small feature.

In my experience, ViewComponents and Turbo are a great mix (made even better with some TailwindCSS sprinkled in), and this article is a great place to see that in action.

If I haven’t sold you on ViewComponents with my blatant gushing, you might appreciate Jason’s more thoughtful take on them. In this article, Jason shares his thoughts on ViewComponents and why he thinks they’re valuable.

From his article —

…since I started using ViewComponent I’ve had a much easier time working with views that have non-trivial logic. In those cases, I just create a ViewComponent and put the logic in the ViewComponent.”

~ ⚒️ HANDY TIP ~ 

→ Alias your component slot helpers with alias

This is a super cool tip for working with ViewComponents — It blew my mind 🤯 when I first read it in @rstankov’s presentation (above).

Rather than having to call #with_slot_name to pass components into ViewComponent slots, we can alias it to #slot_name using alias.

In practice, say you have a TableComponent. Rather than calling TableComponent.with_row, you can alias it to just TableComponent.row. It’s a little thing, but it keeps your code cleaner and easier to follow.

##################
# table.rb
#
class Table < ViewComponent::Base
  renders_many :rows
  alias row with_row    # shorten with alias

  ...
end

##################
# index.html.erb
#
# ✅ now you can write this — 
<%= render Table.new do |table| %>
  <%= table.row(...) %>
<% end %>

# ❌ instead of this — 
<%= render Table.new do |table| %>
  <%= table.with_row(...) %>        # extra with_
<% end %>