DEV Community

Cover image for Ruby on Rails Code Reading Part 2 - Rails Request Processing
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Ruby on Rails Code Reading Part 2 - Rails Request Processing

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

Overview

Recording the code reading work of 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. Implement the index method in the controller
  def index
    binding.pry
    render json: { message: 'Hello World!' }
  end
Enter fullscreen mode Exit fullscreen mode
  1. Set up routing
  get "example" => "example#index"
Enter fullscreen mode Exit fullscreen mode

Code Reading

Follow the flow of how Rails processes requests.

Access http://127.0.0.1:3000/example and use pry-backtrace in the console to view the stack trace.

Due to the large output, it's not possible to follow everything, so let's look at it in a condensed form.

  1. Puma accepts the request and calls the Rails application
  2. railties/lib/rails/engine.rb#L536
    • Definition of Rack API
  3. rack/rack - lib/rack/sendfile.rb#L113
    • Rack application is executed
  4. Resolve routing based on request information
  5. Call the index of the controller

It was more complex than expected, and since I'm not familiar with Ruby, I could only grasp the atmosphere to some extent...

References

Top comments (0)