DEV Community

Cover image for Ruby on Rails Code Reading Part 3 - Invoking WelcomeController
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Ruby on Rails Code Reading Part 3 - Invoking WelcomeController

This article was originally published on bmf-tech.com.

Overview

Documenting the code reading process for Ruby on Rails.

Preparation

  1. Create a new project with rails new RailsCodeReading.
  2. Add the following to the Gemfile:
gem 'pg'
gem 'pry-rails'
gem 'pry-doc'
gem 'pry-byebug'
gem 'byebug'
Enter fullscreen mode Exit fullscreen mode
  1. Run bundle config set path '.bundle' and then execute bundle install.
  2. rails generate controller Example
  3. Add binding.pry to railties/lib/rails/welcome_controller.rb#L9:
  def index
    binding.pry
  end
Enter fullscreen mode Exit fullscreen mode

Code Reading

Let's explore how WelcomeController#index is invoked.

WelcomeController#index is not defined in config/routes.rb and seems to be defined by default, likely due to the autoload mechanism.

railties/lib/rails.rb#L33

By being autoloaded here, WelcomeController is set in the routing.

Here is the implementation of WelcomeController.
railties/lib/rails/welcome_controller.rb#L5

Although I couldn't follow the code in detail, it seems that by leveraging the autoload mechanism, routing is resolved without explicitly registering it.

Top comments (0)