How to realize a skinny controller

Tomoharu Tsutsumi
1 min readSep 6, 2020

When you code ruby on rails, you might sometimes add new actions except for basic rails actions(index, new, create, show, edit, update, destroy). Ideally, you should use only 7 basic actions in a controller (by DHH). When I heard the claim for the first time, I was skeptical of it. However, basically you can realize the skinny controller by dividing it.

case study

If you are developing an application in which users can manage their books, you might use a controller like this.

BooksController < ApplicationControllerdef index
@books = Book.all
end
def show
@book = Book.find(params[:id])
end
def publish #<= this
end
def unpublish #<= this
end

In this case, you should add a new controller. That is below.

Books::PublicationsController <  ApplicationControllerdef update
end
def destroy
end

Instead of publish method, you can use update, moreover, you can use destroy instead of unpublish. The controller became skinny.

Routing is like this.

resources :books do
resource :publication, only: [:update, :destroy], module: "books"
end

Started LinkedIn as well!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi
Tomoharu Tsutsumi

Written by Tomoharu Tsutsumi

5+ years Full Stack SWE (Ruby, Go, TypeScript, JavaScript) | Former Founding Engineer of AI Startup in Canada

No responses yet