Building a simple commenting form with Stimulus and Rails 6

If you use Ruby on Rails, you’ve probably heard of Stimulus, a “modest” JavaScript framework for sprinkling in small bits of JavaScript to your mostly-HTML front-end. Instead of reaching for the monster JavaScript frameworks that have become very popular in recent years, Stimulus gives you the tools to add just enough interactivity while relying on plain-old Rails and HTML as often as possible.

To show you the power and simplicity of Stimulus we’re going to build a simple Stimulus-powered implementation of a commenting form. When we’re finished we’ll have a comment form that skips a page turn and neatly renders our new comments as we create them, without jQuery or JS partials — just a little bit of Stimulus-flavored JavaScript and HTML. I’m going to walk through this step-by-step from a new Rails 6 project. If you want to follow along from an existing project that already has Stimulus and rails-ujs installed, skip ahead to the Creating our comment form section.

I’m writing this assuming that you’ve already got Rails installed on your machine and that you are generally familiar with Rails and Javascript. If you’re just getting started with either, you can still follow along, but you might find some things confusing!

I’m also not going to do a full overview of how Stimulus works, since the official docs do that well. Instead, our Stimulus work will be focused on integrating Stimulus with Rails.

Before we start, a note: Since this tutorial was written, Stimulus 2 has been released. The below code will all work just fine with Stimulus 2; however, you’ll get some deprecation warnings in your web console. The companion repository for this tutorial has been updated for Stimulus 2 if you’d like to see what has changed in Stimulus 2.

The Setup

First, let’s create our Rails project and setup our database, from our terminal:

rails new stimulus_comments_demo --webpack=stimulus
cd stimulus_comments_demo
rails db:create

The --webpack=stimulus here is the important part. This will ensure that our new project is set up correctly and save us a lot of copy/pasting boilerplate later.

Our demo application will have Projects and each Project will have any number of comments.

Let’s setup the basics of that structure next:

rails generate scaffold project name:string
rails generate model comment body:text project:references
rails db:migrate

Now that our project has resources, let’s update our routes.rb file to set a root path:

Rails.application.routes.draw do
  resources :projects
  root to: 'projects#index'

Finally, our Projects have many comments — let’s add that association to our project model

class Project < ApplicationRecord
  has_many :comments

Whew — the Rails setup work is out of the way and we now have a project ready for our new stimulus comment form.

Verify everything is working by booting up your rails server and visiting localhost:3000 . You should see our unstyled projects page. Go ahead and add a new Project while you’re there since we’ll need that later.

A screenshot of the comment form that we just created

Creating our comment form

With our Projects project setup, we can start building out comments. Let’s start by setting up our Rails controller for comments and adding views to support comments, from our terminal:

rails g controller Comments
touch app/views/comments/_show.html.erb
touch app/views/comments/_form.html.erb

Our Rails controller for comments will handle creation of new comments and, if you’ve ever handled a form POST in Rails, it will look pretty familiar.

class CommentsController < ApplicationController
  before_action :set_project

  def create
    comment =
    comment.project = @project
      # Instead of redirecting like we normally would, we render a partial for our Stimulus controller to handle

      # Note that we're just rendering HTML here, no need to render jbuilders or js partials

      render partial: 'show', locals: { comment: comment }, status: :ok
      render partial: 'form', locals: { comment: comment }, status: :unprocessable_entity


  def comment_params

  def set_project
    @project = Project.find(params[:project_id])

Before we move on to our views, let’s update our routes file so that our comments controller is accessible.

Rails.application.routes.draw do
  resources :projects do
    resources :comments, only: [:create]
  root to: 'projects#index'

Next, we’ll add content to our comment view files, and update our project show view to render comments for the project.

<!-- app/views/projects/show.html.erb -->
<div class="comments" data-controller="comments">
    <%= %>
  <div data-target="comments.commentList">
    <% @project.comments.each do |comment| %>
      <%= render partial: "comments/show", locals: { comment: comment } %>
    <% end %>
    <%= render partial: "comments/form" %>

<!-- app/views/comments/_show.html.erb -->
  <%= comment.body %>

<!-- app/views/comments/_form.html.erb -->
<%= form_with model:, url: project_comments_path(@project), data: { action: "ajax:success->comments#createSuccess" } do |form| %>
    <%= form.text_field :body, data: { target: "comments.commentBody" }, placeholder: true %>
    <%= form.submit "Save" %>
<% end %>

There are some important concepts in these views. Let’s walk through each one.

Our project show view connects to our Stimulus comments controller on line 2 with data-controller="comments" . The controller HTML element needs to wrap both our comment list and our comment form, otherwise the controller actions and targets on those elements won’t function.

Next, on line 7, we set our first Stimulus target with data-target="comments.commentList" . We’ll use this target in our Stimulus controller when we insert newly created comments into the DOM.

Finally, we connect our form to our Stimulus controller with data: { action: "ajax:success->comments#createSuccess on line 23.

This action listens for ajax:success events and calls the createSuccess method when an ajax:success event occurs. Remember that, by default, forms created with form_with are remote, which we want. Also note the data-target="comments.commentBody" on line 25, which we’ll use in our controller to clear the text field after submission.

At this point, if we visit a project’s show page in our Rails app and submit our comment form the form submission will work just fine, you will just have to reload the page before you’ll see each new comment.

Adding the Stimulus Controller

Our Stimulus controller is the last piece we’re missing, and it is simpler than you might imagine.

Let’s start by creating our Stimulus controller:

touch app/javascript/controllers/comments_controller.js

And add in the code we need to process the HTML that Rails sends us each time a comment is successfully POSTed to the server:

import { Controller } from "stimulus"

export default class extends Controller {
  // Read more about targets in the Stimulus reference guide:

  static targets = ["commentList", "commentBody"]

  // The connect lifecycle method runs each time our controller is connected to the DOM

  // Read more about lifecycle methods in the Stimulus reference guide:

  connect() {
    console.log("We're connected!")

  createSuccess(event) {
    // This might look a little magical but its just deconstructing the standard rails-ujs event detail

    // Read more here:

    const [_data, _status, xhr] = event.detail

    this.commentListTarget.innerHTML = xhr.response + this.commentListTarget.innerHTML
    this.commentBodyTarget.value = ''

That’s it — that’s the whole Stimulus controller and now we’re handling comment creation without a page turn, like this:

A screen recording showing successful submission of a comment on the form we just built

How does this all just work? The magic of Rails is doing a lot of heavy lifting behind the scenes so that we can listen for ajax events and respond to them in our Stimulus code.

Specifically, rails-ujs adds event handlers for us, making it easy to handle ajax events in our JavaScript code. With rails-ujs notifying us of ajax events, Stimulus kicks in through the data-action set on the form so that we can respond to the ajax event(s) we care about and run JavaScript to process the HTML generated by our Rails server. Mostly though, magic.

If all you were looking for is how to use rails-ujs and Stimulus to handle form POSTs in Rails, you’ve reached the end of the line. Read on for a bonus guide on Tailwind CSS setup and animating our comments.

Bonus: making things fancy with Tailwind CSS and animations

While adding commenting is nice, everything is more fun when it looks and feels nicer. Let’s add a little style to our project with Tailwind CSS and, for fun, look at one way to animate the entry of our comments into the DOM, instead of having them barge in unannounced.

Adding Tailwind to our project

Tailwind is a wonderful CSS framework that is rapidly gaining popularity in the web development world. While we could add styles without a framework, setting up Tailwind is worth the effort.

First, let’s install Tailwind and add it to our project, from our terminal:

yarn add tailwindcss
mkdir app/javascript/stylesheets
yarn tailwind init app/javascript/stylesheets/tailwind.js
touch app/javascript/stylesheets/application.scss

Next, in our application.scss file, we need to pull in Tailwind’s base styles:

@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

Finally, we need to tell webpack about our application.scss file by adding import "stylesheets/application" to app/javascripts/packs/application.js.

Since we haven’t seen it before, let’s look at the full application.js file:

// This file is automatically compiled by Webpack, along with any other files

// present in this directory. You're encouraged to place your actual application logic in

// a relevant structure within app/javascript and only use these pack files to reference

// that code so it'll be compiled.


// Uncomment to copy all static images under ../images to the output folder and reference

// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)

// or the `imagePath` JavaScript helper below.


// const images = require.context('../images', true)

// const imagePath = (name) => images(name, true)

import "controllers"

import "stylesheets/application"

Everything in this file is boilerplate generated by Rails when we setup our project except for line 21, which we just added.

Almost there — now let’s update our postcss file to tell it about our Tailwind configuration file by adding require('tailwind.css')('app/javascript/stylesheets/tailwind.js') to postcss.config.js. Again, let’s take a look at this file in full to make sure we’ve got it setup correctly.

let environment = {
  plugins: [
      autoprefixer: {
        flexbox: 'no-2009'
      stage: 3

module.exports = environment

Yours might be a little different — the Rails default is to update module.exports directly instead of assigning the configuration to a variable first. Either way, just add the content of line 3 to the plugins array and you’re all set.

Next, fire up your webpack-dev-server with bin/webpack-dev-server from your terminal so that changes get compiled as you work.

Okay — last step before we can start styling things. We need to update the application layout to tell it about the new stylesheet pack that webpack is creating for us by replacing the stylesheet_link_tag in the default layout with stylesheet_pack_tag.

Here’s what your application layout should look like:

<!DOCTYPE html>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

    <%= yield %>

Now we’re all set to use Tailwind to add a little style to our project. Tailwind’s documentation is exceptional so I’m not going to cover how Tailwind works. Instead, I’ll just add the markup I used, all of which is just standard Tailwind classes.

Here’s our styled Project show markup:

<div class="container mx-auto max-w-lg border border-gray-200 p-4 mt-4 rounded">
    <h2 class="text-2xl mb-4"><%= %></h2>
  <div data-controller="comments">
    <h3 class="text-xl mb-2">Comments</h3>
    <div data-target="comments.commentList" class="mb-4">
      <% @project.comments.order(created_at: :desc).each do |comment| %>
        <%= render partial: "comments/show", locals: { comment: comment } %>
      <% end %>
      <%= render partial: "comments/form" %>

And our comment show partial:

<div class="mb-2 border-b border-gray-200 p-2">
  <%= comment.body %>

And, finally, our form:

<%= form_with model:, url: project_comments_path(@project), data: { action: "ajax:success->comments#createSuccess" } do |form| %>
  <div class="mb-4">
    <%= form.text_field :body, class: "shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline", data: { target: "comments.commentBody" }, placeholder: "Type your comment..." %>
    <%= form.submit "Save", class: "bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded cursor-pointer" %>
    <% end %>

All put together, we get a Project show page that looks like this:

A screenshot of the comment form we've built with updated styles added in the last section

Animating comment creation

While our comments get added to the DOM right away, they appear instantly, which can feel a little jarring. Let’s wrap up by animating the entry of each new comment into the DOM by first making some small updates to the createSuccess method in our comments_controller.js file.

createSuccess(event) {
  const [data, status, xhr] = event.detail
  const target =

  // Create a wrapper div to hold our newly created comment

  const newComment = document.createElement("div")
  // Add our animation class to the wrapper div and set the innerHTML to the comment rendered by the server

  newComment.innerHTML = xhr.response

  // Add our wrapped comment to the beginning of the comment list

  this.commentBodyTarget.value = ''

  // After the animation finishes (in 0.6 seconds), remove our animation class

  // If we don't remove the animation class, adding another comment will cause every comment added since our initial page load to animate together

  setTimeout(() => {
  }, 600);

Our fade-in-left class isn’t functional yet because we haven’t added any CSS to it. We’ll add that next, with some help from so we don’t have to write our own animation.

Let’s add the following to our application.scss file, so we don’t need to create a new file as we near the end of our time together:

.fade-in-left {
  -webkit-animation: fade-in-left 0.6s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
          animation: fade-in-left 0.6s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
  /* ----------------------------------------------
  * Generated by Animista on 2020-10-15 23:32:49
  * Licensed under FreeBSD License.
  * See for more info.
  * w:, t: @cssanimista
  * ---------------------------------------------- */
  * ----------------------------------------
  * animation fade-in-left
  * ----------------------------------------
  @-webkit-keyframes fade-in-left {
          0% {
            -webkit-transform: translateX(-50px);
                    transform: translateX(-50px);
            opacity: 0;
          100% {
            -webkit-transform: translateX(0);
                    transform: translateX(0);
            opacity: 1;
        @keyframes fade-in-left {
          0% {
            -webkit-transform: translateX(-50px);
                    transform: translateX(-50px);
            opacity: 0;
          100% {
            -webkit-transform: translateX(0);
                    transform: translateX(0);
            opacity: 1;

With those changes in place, each newly added comment now politely animates into the DOM:

A screen recording of the comment form. When a comment is submitted, it fades in from the left side of the list as described in the last section.

Wrapping up

Together, we looked at how you can use the power of rails-ujs and Stimulus to add a little bit of interactivity to your Rails apps without needing to rely on bulky front-end frameworks or clunky JS partials and jQuery solutions. Stimulus + Rails can deliver simple, clean, and interactive experiences for your users, while retaining all of the joys that come with building standard Rails apps.

Here’s a preview of what you can build with just a little bit more Stimulus than what we worked on today— all of the interaction you see in the gif below is built with Stimulus + rails-ujs:

A screen recording of a web app. A user clicks on a link and a sliding drawer opens from the right side of the screen.

Thanks for reading!

Additional Resources: