How to realize a skinny controller
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
enddef show
@book = Book.find(params[:id])
enddef publish #<= this
enddef unpublish #<= this
end
In this case, you should add a new controller. That is below.
Books::PublicationsController < ApplicationControllerdef update
enddef 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