Application 2024-09-02

Ruby on Rails Code Reading Part 2 - Rails Request Processing

Recording the code reading work of Ruby on Rails.

Read in: ja
Ruby on Rails Code Reading Part 2 - Rails Request Processing

Overview

This document records the code reading work 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', then execute bundle install.
  2. Run rails generate controller Example.
  3. Implement the index method in the controller:
  def index
    binding.pry
    render json: { message: 'Hello World!' }
  end
  1. Set up the routing:
  get "example" => "example#index"

Code Reading

Let's trace how Rails processes requests.

By accessing http://127.0.0.1:3000/example and using pry-backtrace in the console, you can see the stack trace.

Since a lot of output is generated, we can't follow everything, so we'll skip through it.

  1. Puma accepts the request and calls the Rails application.
  1. Based on the request information, the routing is resolved.
  1. Calls the index of the controller.

It was more complex than I imagined, and since I'm not familiar with Ruby, I could only grasp the general idea...

References

Tags: Ruby Ruby on Rails
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