# How to Debug and Step-Through Rails Codebase

*Published on September 16, 2023*

Tags: Rails Internals

---


> 💡 I originally wrote this post two years ago, which showed the usage of the&nbsp;`pry-byebug`&nbsp;gem. Since then, I've switched to the&nbsp;[`debug`](https://github.com/ruby/debug)&nbsp;gem for all my Ruby debugging needs, and hence re-publishing the updated post.

Reading the&nbsp;[source code](https://github.com/rails/rails)&nbsp;is a great way to understand how a feature works in Rails. However, stepping through the specific Rails method you're interested in is even better. You can inspect the local variables, follow the conditional path, and learn&nbsp;**exactly**&nbsp;what's happening behind the scenes.

This post shows how you can debug and step-through the Rails codebase for a better understanding of the framework.

* * *

Recently, while researching the&nbsp;[authenticity tokens](/how-rails-authenticity-tokens-protect-against-csrf)&nbsp;in Rails, I wanted to learn how Rails verifies the authenticity tokens.

After reading the&nbsp;[API docs](https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html#method-i-verify_authenticity_token), at first glance, the method looks straightforward. Upon receiving a request, Rails checks if the request is verified and calls the&nbsp;`handle_unverified_request`&nbsp;method if it isn't.

```ruby
# actionpack/lib/action_controller/metal/request_forgery_protection.rb

def verify_authenticity_token
  mark_for_same_origin_verification!

  if !verified_request?
    logger.warn unverified_request_warning_message if logger && log_warning_on_csrf_failure

    handle_unverified_request
  end
end
```

Reading the source code of the&nbsp;`verified_request?`&nbsp;method reveals that a few different checks need to happen to verify a request.

```ruby
def verified_request? # :doc:
  !protect_against_forgery? || request.get? || request.head? ||
    (valid_request_origin? && any_authenticity_token_valid?)
end
```

Since Ruby is so readable, you can still read the following code and understand what's going on, but it would be sweet to step inside each method and understand precisely how Rails verifies a request.

**How should you follow the execution path for a particular request?**

## Let's Step-Through the Source Code

Let's say I want to debug and walk-through the&nbsp;`verified_request?`&nbsp;method. For this, I need to open the source file where that method is defined and insert a breakpoint in it.

### Step 1: Open the source file

To open the relevant gem, go to your application in the terminal and run the&nbsp;`bundle open gem_name`&nbsp;command. Since the&nbsp;`verified_request?`&nbsp;method is defined in the&nbsp;`RequestForgeryProtection`&nbsp;module, which is defined in the ActionPack gem, I will open that using the following command.

```bash
$ bundle open actionpack
```

Assuming you have configured the&nbsp;`EDITOR`&nbsp;environment variable, Ruby will open the above gem in the editor so you can inspect the code (I use VS Code). Now you can navigate to the file containing the code you're interested in.

![open rails source in editor](https://writesoftwarewell.com/media/q40txxuuuuvilbh19d1zj5gkm7qb.png "open rails source in editor")

### Step 2: Insert a breakpoint

For debugging the method, we need to insert a breakpoint. For this, I will use the&nbsp;`debug`&nbsp;gem.&nbsp;[Rails includes this gem out of the box](https://github.com/rails/rails/blob/main/Gemfile#L144). For any other codebase, you can run&nbsp;`bundle add debug`&nbsp;to install and add it to your gemfile. Once set up, you can insert a breakpoint using the&nbsp;`debugger`&nbsp;statement at the beginning of the method.

```ruby
def verified_request?
  debugger
  
  !protect_against_forgery? || request.get? || request.head? ||
    (valid_request_origin? && any_authenticity_token_valid?)
end
```

That's it. Save the file and restart your application.

### Step 3: Step-through the Method

After restarting the application, Rails will load the modified source code containing our breakpoint. Now, whenever you hit the corresponding URL that invokes the feature you're trying to step-through (or debug), Ruby will pause the execution wherever you have placed the breakpoint.

![paused-at-breakpoint.png](https://writesoftwarewell.com/media/06c6a8zgxut4f5p6awinu1wnxxab.png "Paused at Breakpoint")

At this point, you are inside the Rails codebase. You can step through the entire Rails source code as you'd in your regular application.

- To step into a method, type&nbsp;`s`,
- To step out of a method, type&nbsp;`u`,
- To continue, type&nbsp;`c`, etc.

And of course, you have access to all local variables as usual. Just type the name of the variable and the debugger will show you its value. For detailed instructions on using the debugger, check out&nbsp;[`debug`&nbsp;gem's documentation.](https://github.com/ruby/debug)

