This idea is heavily influenced by CQRS and its way of applying changes to the app via commands objects. In this blogpost we’re showing how it could work with Rails.

Struggling with finding Senior Ruby developers? - Show your job post here and reach thousands of developers quickly.

Commands are data structures which represent the intention of the user. In the Rails community, we sometimes use the name of “a form object” to represent the same meaning.

In some of our projects, we started moving to a command-driven approach. A command is handled by a command handler (often it’s a service object). As a result of handling the command we publish domain events.

When you switch to commands, you’ll notice that many controllers look alike and they’re becoming a boiler-plate code which you repeat over and over.

This is what led to a conversation between me and Paweł. We discussed whether it makes sense to have just one controller, being represented by just one API endpoint.

Paweł decided to experiment with this idea and wrote the code below. This code is also a nice example of how concise can be a one-file-Rails application.

require 'action_controller/railtie'
require 'securerandom'

module Command
  Error =

module Service
  Error =

FooBarCommand = do
  ValidationError =
  def validate!
    raise ValidationError if [foo, bar].any? { |value| value.nil? || value == '' }

class FooBarService
  FooNotFooError =
  def call(cmd)
    raise FooNotFooError unless == 'foo'

  { 'foo_bar' => FooBarCommand,

  { FooBarCommand =>,

class Dummy < ::Rails::Application
  config.eager_load = false
  config.secret_key_base = SecureRandom.hex

class CommandsController < ActionController::Base
  def create
    cmd =
    head :no_content
  rescue KeyError
    render json: {}, status: :not_found
  rescue Command::Error
    render json: {}, status: :unprocessable_entity
  rescue Service::Error
    render json: {}, status: :unprocessable_entity

Dummy.routes.draw do
  resources :commands, only: :create
Dummy.routes.default_url_options[:host] = ''

run Rails.application

# How to run this example:
# rackup
# http POST localhost:9292/commands command=foo_bar foo=foo bar=bar

I really like this concept. I think it has the potential of removing a lot of controller code.

If this can work in some cases, this idea would become the most radical one in my book on dealing with Rails controllers. In the current version of the book, we talk a lot about the concept of form objects as a data structure which is initialized in the controller and passed to the service object.

The approach with a generic controller handling commands removes a big part of the controller layer.

Knowing Paweł, there will be updates and improvements to this approach, so stay tuned :)