How to use Alpine.js with Ruby on Rails

Updated: 

Learn how to integrate Alpine.js with Ruby on Rails for simple, interactive UIs. Discover its benefits and streamline your frontend without heavy frameworks.

Alex Sinelnikov

Alex Sinelnikov

Follow me on Twitter for more interesting content.
As a Ruby on Rails developer, you're used to the "convention over configuration" philosophy and the power of a full-stack framework. But when it comes to adding interactive elements to your frontend, you might feel torn between the simplicity of vanilla JavaScript and the robustness of heavy JavaScript frameworks.

Enter Alpine.js – a lightweight JavaScript framework that offers the perfect balance for Rails developers. In this article, we'll explore why Alpine.js is an excellent choice for Rails projects and why you might not need a separate UI framework at all.

The Rails Way Meets Modern JavaScript


Ruby on Rails has always been about developer productivity and happiness. Alpine.js shares this philosophy by providing a minimal yet powerful set of tools to create dynamic interfaces. Here's why it's a great fit for Rails developers:

  1. Minimal Learning Curve: Alpine.js uses simple directives that feel familiar to anyone who has worked with Rails' ERB templates. You can start using it productively within minutes.
  2. Progressive Enhancement: Like Rails' Turbolinks, Alpine.js allows you to enhance your server-rendered HTML progressively. This aligns perfectly with the Rails approach to web development.
  3. No Build Step Required: Unlike many modern JavaScript frameworks, Alpine.js doesn't require a complex build process. You can include it with a single <script> tag, making it easy to integrate into your Rails asset pipeline.
  4. Plays Well with Turbolinks: Alpine.js works seamlessly with Turbolinks, maintaining state and event listeners even after page changes.
  5. Keeps Your Rails Views Clean: With Alpine.js, you can add interactivity directly in your HTML, keeping your JavaScript separate and your Rails views clean and readable.

Why You Might Not Need a Separate UI Framework


When using Alpine.js with Rails, you may find that you don't need a separate UI framework at all. Here's why:

  1. Rails Has Great Helpers: Rails comes with a rich set of view helpers that can generate most of the HTML structure you need. Alpine.js can then add the necessary interactivity.
  2. CSS Frameworks Are Enough: Most UI needs can be met with a good CSS framework like Bootstrap or Tailwind CSS in Rails. Alpine.js can handle the interactive parts, eliminating the need for JavaScript-heavy UI frameworks.
  3. Easier Maintenance: With fewer moving parts and external dependencies, your application becomes easier to maintain and upgrade over time.

Getting Started with Alpine.js in Your Rails Project


Adding Alpine.js to your Rails project is straightforward. Here's a quick guide:

1. Add the Alpine.js script to your application layout:

 <%= javascript_include_tag 'https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js', defer: true %>


2. Start using Alpine.js directives in your views. For example, to create a simple dropdown:

 <div x-data="{ open: false }">
   <button @click="open = !open">Toggle Dropdown</button>
   <ul x-show="open" @click.away="open = false">
     <li><%= link_to 'Option 1', '#' %></li>
     <li><%= link_to 'Option 2', '#' %></li>
     <li><%= link_to 'Option 3', '#' %></li>
   </ul>
 </div>


Conclusion


Alpine.js offers Ruby on Rails developers a pragmatic approach to adding interactivity to their applications. It aligns well with the Rails philosophy, integrates easily into existing projects, and often eliminates the need for heavier UI frameworks. By leveraging the power of Rails helpers, and Alpine.js for lightweight interactivity, you can create modern, responsive web applications without straying far from the Rails ecosystem you know and love. For more complex cases you can use Turbo frames to replace parts of UI and use alpine for interactivity, it works well together.

Give Alpine.js a try in your next Rails project – you might be surprised at how much you can accomplish with so little.

Follow me on Twitter for more interesting content.