Aug 6, 2018

My option on application_thing.rb vs base_thing.rb

I recently wrote about application_things and how having them will steer you into building more maintainable Rails applications. But I want to talk about going deeper.

Let’s say you have a path of /my in your routes. /my has a lot of things, like a controller to manage credit cards, invoices, stuff like that.

|- app
|--- controllers
|------ application_controller.rb
|------ my
|--------- credit_cards_controller.rb
|--------- invoices_controller.rb

Don’t have these controllers inherit from application_controller.rb — they should act as a subset of My, and My should be treated differently than your overall application.

Create a base_controller.rb in my and have that inherit from applicaiton_controller.rb. Some developers will do this but also call it application_controller.rb, but I find doing so odd, because we’re inheriting an ApplicationController from an ApplicationController, blah blah blah…

module My
  class BaseController < ApplicationController
    before_action :authenticate_user!

And then inherit your credit_cards_controller.rb from BaseController.