Application 2024-09-04

Ruby on Rails Code Reading Part 3 - Invoking WelcomeController

Documenting the code reading process for Ruby on Rails.

Read in: ja
Ruby on Rails Code Reading Part 3 - Invoking WelcomeController

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'
  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

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.

Tags: Ruby on Rails Ruby
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles