Guides

Building Rails Hotwire and Turbo patterns with Rails 8 an...

This guide provides a step-by-step implementation strategy for modernizing a Rails application with Hotwire, deploying with Kamal, and integrating background job processing. Focuses on practical architecture decisions and production constraints.

2-3 hours6 steps
1

Audit application for Hotwire compatibility

Identify JavaScript dependencies and Turbo Frame usage patterns. Check for jQuery plugins that require replacement with Stimulus controllers or Hotwire behaviors.

grep -r 'jquery' app/javascript/packs
rails generate stimulus:controller turbo
rails generate stimulus:controller hotwire
2

Update Gemfile for Rails 8 compatibility

Replace Rails 7 gems with Rails 8 equivalents. Update Active Job backend to use Solid Queue for background processing.

Gemfile
gem 'rails', '8.0.0'
gem 'solid_queue', '~> 0.6.0'
gem 'bootsnap', require: false

⚠ Common Pitfalls

  • Ensure all gems in Gemfile.lock are compatible with Ruby 3.1+
  • Check for deprecated Active Record query methods
3

Configure Kamal deployment pipeline

Create production deployment configuration with Kamal. Set up environment variables for database connections and secret keys.

kamal.yml
production:
  app: my-rails-app
  repo: [email protected]:username/my-rails-app.git
  branch: main
  secret_key_base: ENV['SECRET_KEY_BASE']
4

Implement Turbo Streams for real-time updates

Replace AJAX calls with Turbo Streams. Create partials for DOM updates and add Turbo Stream tags to views.

app/views/comments/_comment.html.erb
<%= turbo_stream_from "comments_#{post.id}" %>
<%= turbo_stream.replace "comment_#{@comment.id}", partial: 'comments/comment', locals: { comment: @comment } %>
5

Set up Sidekiq with Redis for background jobs

Configure Sidekiq middleware and Redis connection. Add job classes to Active Job queue with proper retry policies.

app/jobs/comment_notification_job.rb
class CommentNotificationJob < ApplicationJob
  queue_as :default
  retry_on StandardError, wait: 5.seconds, attempts: 3
  def perform(comment_id)
    CommentMailer.notify(comment_id).deliver_now
  end
end

⚠ Common Pitfalls

  • Ensure Redis connection pool size matches concurrent job processing needs
  • Avoid long-running jobs in default queue
6

Create system tests with RSpec and FactoryBot

Write integration tests for Hotwire interactions. Use FactoryBot to create test data for complex scenarios.

spec/factories/comments.rb
FactoryBot.define do
  factory :comment do
    body { 'Test comment' }
    post
  end
end

What you built

Verify all Hotwire components work without JavaScript, confirm Kamal deployment process, and ensure background jobs process reliably. Run full test suite with coverage report before production release.